private void Update() { Move(MoveVector); if (lockInBoundary) { SceneBoundary.MoveBackToBounds(transform); } }
private void Awake() { if (_instance != null && _instance != this) { Destroy(gameObject); } _instance = this; _isInstanceNull = false; _collider = GetComponent <Collider2D>(); _bounds = _collider.bounds; }
private static SceneBoundary GetInstance() { if (_isInstanceNull) { GameObject obj = new GameObject("Boundary Box"); obj.transform.position = Vector3.zero; BoxCollider2D coll = obj.AddComponent <BoxCollider2D>(); coll.size = DefaultSize; coll.isTrigger = true; _instance = obj.AddComponent <SceneBoundary>(); } return(_instance); }
public bool PointIn(Vector3 point) { if (this.Count < 3) { return(false); } else { if (inScene) { return(SceneBoundary.PointIn(point)); } } return(false); }
// Update is called once per frame private void FixedUpdate() { timeCounterPerceive -= Time.fixedDeltaTime; if (timeCounterPerceive < 0f) { timeCounterPerceive = perceiveInterval; GameObject otherObject; Collider2D[] listCollision = Physics2D.OverlapCircleAll((Vector2)this.transform.position, interationRadius * transform.localScale.y); bool isNearSomeone = false; for (int i = 0; i < listCollision.Length; i++) { if (listCollision[i].gameObject != this.gameObject && listCollision[i].gameObject.tag != "scene_boundary") { isNearSomeone = true; break; } } SetBehaviour((isNearSomeone && socialManager.SocialFactor > 0) ? Behaviour.CHATTING : Behaviour.WANDERING); desireVelocity.x = 0f; desireVelocity.y = 0f; if (behaviour == Behaviour.WANDERING) { //if (socialManager.SocialFactor > 0) { // re-calculate desire velocity SocialManager otherSocialManager; listCollision = Physics2D.OverlapCircleAll((Vector2)this.transform.position, detectPeopleRadius * transform.localScale.y); for (int i = 0; i < listCollision.Length; i++) { otherObject = listCollision[i].gameObject; if (otherObject == this.gameObject) { continue; } if (otherObject.tag == "scene_boundary") { SceneBoundary sceneBoundary = otherObject.GetComponent <SceneBoundary>(); if (sceneBoundary) { Vector2 delta = Vector2.zero; switch (sceneBoundary.BoundaryType) { case SceneBoundary.Type.TOP: case SceneBoundary.Type.BOTTOM: delta.y = this.transform.position.y - otherObject.transform.position.y; break; case SceneBoundary.Type.LEFT: case SceneBoundary.Type.RIGHT: delta.x = this.transform.position.x - otherObject.transform.position.x; break; } desireVelocity += delta.normalized * avoidObstacleFactor / delta.sqrMagnitude; } } else { otherSocialManager = otherObject.GetComponent <SocialManager>(); if (otherSocialManager) { desireVelocity += 1 / CalculateEulerDistance(socialManager.CharacteristicVector, otherSocialManager.CharacteristicVector) * ((Vector2)(otherObject.transform.position - this.transform.position)).normalized * socialManager.SocialFactor; } } } } if (desireVelocity.sqrMagnitude == 0f) { desireVelocity.x = Random.Range(-1.0f, 1.0f); desireVelocity.y = Random.Range(-1.0f, 1.0f); } desireVelocity = desireVelocity.normalized * maxSpeed; } else if (behaviour == Behaviour.CHATTING) { // nothing happen } } steeringForce = rb2d.mass * (desireVelocity * transform.localScale.y - rb2d.velocity); if (steeringForce.sqrMagnitude > maxSteeringForce * maxSteeringForce) { steeringForce = steeringForce.normalized * maxSteeringForce; } rb2d.AddForce(steeringForce * transform.localScale.y); }