Пример #1
0
    void LateUpdate()
    {
        //no point moving if there are no waypoints
        if (waypoints.length == 0)
        {
            return;
        }

        //perform a 2D lookat - change to 3D for things that moves in 3D.e.g.airplanes or birds - waypoints[currentWP].transform.position.y
        Vector3 lookAtGoal = new Vector3(waypoints[currentWP].transform.position.x, this.transform.position.y, waypoints[currentWP].transform.position.z);
        Vector3 direction  = lookAtGoal - this.transform.position;

        this.transform.rotation = Quarternion.Slerp(this.transform.rotation, Quarternion.LookRotation(direction), Time.deltaTime * rotSpeed);

        //if close enough to a waypoint: increase goal waypoint by 1
        if (direcion.magnitude < accuracy)
        {
            currentWP++;

            //cycle around the waypoints
            if (currentWP >= waypoints.Length)
            {
                currentWP = 0;
            }
        }
        //never stop at anything
        this.transform.Translate(0, 0, speed * Time.deltaTime);
    }
Пример #2
0
    void LateUpdate()
    {
        Vector3 lookAtGoal = new Vector3(goal.position.x, this.transform.position.y, goal.position.z);

        //work out the direction to face towards the goal vector
        Vector3 direction = lookAtGoal - this.transform.position;

        //snap to the right direction
        //this.transform.LookAt(lookAtGoal);

        //using quarternion - a complex data structure holding a rotation - to do fantastic things
        //turning an angle from facing in one direction to another: takes the current character rotation, perform LookRotation on direction vector
        //LookRotation gives the rotation that needs to occur to be facing in the direction

        this.transform.rotation = Quarternion.Slerp(this.transform.rotation, Quarternion.LookRotation(direction), rotSpeed * Time.deltaTime);

        //if using applied root motion: remove the code below - related to pushing the character forward with animation rather than code
        if (Vector3.Distance(transform.position, lookAtGoal) > accuracy)
        {
            this.transform.Translate(0, 0, speed * Time.deltaTime);
        }
    }