// Start is called before the first frame update void Start() { wayPointNodesManager_ = FindObjectOfType <WayPointNodesManager>(); currentWayPointNode_ = wayPointNodesManager_.GetClosestWayPointNode(transform.position); body_ = GetComponent <Rigidbody2D>(); }
private void Add(int x, int y) { var wayPointNode = new WayPointNode(x, y); if (_start == null) { _last = _start = wayPointNode; } else { _last = _last.SetNext(wayPointNode); } }
//Reset the lost kitten private void ResetLostKitten() { transform.position = startPos; target = firstWayPointNode; steeringForce = Vector3.zero; steering.Speed = 0; }
//Restart the seeker to the starting position if it collides with //the target private void OnTriggerEnter(Collider collider) { if (!capturedByPlayer) { //If collided with a waypoint, then seek the next waypoint if (collider.gameObject.tag == "WayPoint") { //If it is the last waypoint, then start both //the player and the lost kitten back at the start position if (collider.gameObject.GetComponent<WayPointNode>().nextWayPointNode == null) { Reset(); } else { target = target.nextWayPointNode; } } } }
public void Start() { //get component reference characterController = gameObject.GetComponent<CharacterController>(); steering = gameObject.GetComponent<Steering>(); moveDirection = transform.forward; steering.maxForce = 90; // tags behave as layers, but are called tags (and can't be used for masks) obstacles = GameObject.FindGameObjectsWithTag ("Obstacle"); avoidDist = obstacles[0].GetComponent<Dimensions>().Radius + 10; //Find the player player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); //Find the mom cat momCat = GameObject.FindGameObjectWithTag("Mom Cat"); //Assign the target to be the first way point target = firstWayPointNode; //Set up everything else startPos = transform.position; capturedByPlayer = false; BehindPointRadius = BehindPoint.GetComponent<SphereCollider>().radius + BEHIND_POINT_RADIUS_OFFSET; isSearchingForWayPoint = false; returnedToMomCat = false; //Make the lost kitten not visible at first Visibility(false); }
public WayPointNode SetNext(WayPointNode wayPointNode) { return(Next = wayPointNode); }
// Update is called once per frame void Update() { float distanceToTarget = Vector3.Distance(transform.position, currentWayPointNode_.transform.position); if (distanceToTarget <= stoppingDistance_) { if (clockwise_) { currentWayPointNode_ = currentWayPointNode_.GetNextWayPointNode(); } else { currentWayPointNode_ = currentWayPointNode_.GetPreviousWayPointNode(); } } else { Vector3 dir = (currentWayPointNode_.transform.position - transform.position).normalized; switch (movementType_) { case MovementType.TRANSFORM: { transform.position += Time.deltaTime * speed_ * dir; } break; case MovementType.BODY_VELOCITY: { body_.velocity = dir * speed_; } break; case MovementType.BODY_FORCE: { body_.AddForce(dir); if (body_.velocity.magnitude > speed_) { body_.velocity = body_.velocity.normalized * speed_; } } break; case MovementType.SEEK_BEHAVIOR: { Vector2 desiredVelocity = (currentWayPointNode_.transform.position - transform.position).normalized * speed_; Vector2 steering = desiredVelocity - body_.velocity; if (steering.magnitude > maxForce_) { steering = steering.normalized * maxForce_; } steering /= body_.mass; body_.AddForce(steering); if (body_.velocity.magnitude > speed_) { body_.velocity = body_.velocity.normalized * speed_; } } break; case MovementType.PATH_BEHAVIOR: { Vector2 desiredVelocity = (currentWayPointNode_.transform.position - transform.position).normalized * speed_; Vector2 steering = desiredVelocity - body_.velocity; if (steering.magnitude > maxForce_) { steering = steering.normalized * maxForce_; } steering /= body_.mass; body_.AddForce(steering); float maxSpeed = speed_; if (distanceToTarget < arrivalDistance_) { maxSpeed = Mathf.Lerp(0, maxSpeed, distanceToTarget / arrivalDistance_); } if (body_.velocity.magnitude > maxSpeed) { body_.velocity = body_.velocity.normalized * maxSpeed; } } break; default: throw new ArgumentOutOfRangeException(); } } }