如何在Hierarchy中创建ThirdPersonController数组并添加Waypoint?

时间:2016-07-27 16:16:47

标签: c# unity3d unityscript unity5

我所做的是添加了一个ThirdPersonConctroller然后在菜单中我点击了Window>动画。创建空状态称为Walk。州设置为HumandoidWalk。

然后我在Hierarchy中添加了另一个ThirdPersonController,并在x上添加了不同的位置。现在当游戏运行时,ThirdPersonController都在行走。

现在我将第二个ThirdPersonController移动速度更改为2,因此它比第一个移动速度更快。

现在我想知道如何做两件事:

如果我想在Hierarchy中制作10个ThirdPersonController,我可以从Assets>中反复拖动新的ThirdPersonController。字符> ThirdPersoncharacter>预制件,但这是很多工作。我想知道是否有办法用c#脚本或其他方式来创建ThirdPersonController数组?

我想制作一个航路点,所以当ThirdPersonController到达那里时,他将转向180度并将走回它的原点位置。如果其中一个ThirdPersonController走得更快,他会更快回头。但是这个想法是,ThirdPersonController都会到达x的相同位置并且会返回。

例如,第一个ThirdPersonController在x = 0 y = 0 z = 0时所以当他走路并且到达z = 100时只有z改变然后转回z = 0

另一个ThirdPersonController在x = -1.53​​9 y = 0 z = 0,所以这次也只有当他到100转回z = 0时z变为100

Screenshot

我在c#中有这个工作的航点脚本 问题是,现在两个p层一个接一个地走到了路标上,我想像以前那样第二个玩家走在第一个旁边,比第一个走得快一点。

在我对Inspector中第二个ThirdPersonController(1)的移动速度和位置进行更改之前,将移动速度更改为2并将位置更改为-1.53​​7但是在添加了路标脚本后,他们以相同的速度行走一个接一个。

using UnityEngine;
using System.Collections;

public class Waypoints : MonoBehaviour {

    public Transform[] waypoint;
    public float patrolSpeed;
    public bool loop = true;
    public int dampingLook = 4;
    public float pauseDuration;
    private float curTime;
    private int currentWaypoint = 0;
    public CharacterController character;

    // Use this for initialization
    void Start () {


    }

    void LateUpdate(){

        if(currentWaypoint < waypoint.Length){
            patrol();
        }else{    
            if(loop){
                currentWaypoint=0;
            } 
        }
    }

    void patrol(){

        Vector3 nextWayPoint = waypoint[currentWaypoint].position;

        // Keep waypoint at character's height
        nextWayPoint.y = transform.position.y; 

        // Get the direction we need to move to
        // reach the next waypoint
        Vector3 moveDirection = nextWayPoint - transform.position;


        if(moveDirection.magnitude < 1.5){
            Debug.Log("enemy is close to nextwaypoint");


            // This section of code is called only whenever the enemy
            // is very close to the new waypoint 
            // so it is called once after 4-5 seconds.

            if (curTime == 0)
                // Pause over the Waypoint 
                curTime = Time.time; 

            if ((Time.time - curTime) >= pauseDuration){
                Debug.Log("increasing waypoint");

                currentWaypoint++;
                curTime = 0;
            }
        }
        else
        {     
            Debug.Log("reaching in rotation " + moveDirection.magnitude);
            // This code gets called every time update is called
            // while the enemy if moving from point 1 to point 2.
            // so it gets called 100's of times in a few seconds  

            // Now we need to do two things
            // 1) Start rotating in the desired direction
            // 2) Start moving in the desired direction 

            // 1) Let' calculate rotation need to look at waypoint
            // by simply comparing the desired waypoint & current transform
            var rotation = Quaternion.LookRotation(nextWayPoint - transform.position);

            // A slerp function allow us to slowly start rotating 
            // towards our next waypoint 
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 
                Time.deltaTime * dampingLook);

            // 2) Now also let's start moving towards our waypoint
            character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
        }  
    }
}

0 个答案:

没有答案