有回溯路径的问题

时间:2016-04-08 05:39:27

标签: unity3d unityscript

我是团结的新人。代码的目的是使游戏对象回溯路径。我正在尝试制作一个保存玩家控制的游戏对象路径的代码,在按下A'时,游戏对象重新定位到起点&安培;然后根据保存的路径移动。

代码能够保存输入(使用Debug.log检查),它也会移动到staarting位置。但是根据保存的路径不会移动。

游戏对象始终从(0,0,0)移动到(-0.7,0,0),无论输入如何(即使我没有给出任何输入并且直接按下' A&#39 ;)

我可能犯了一个愚蠢的错误。抱歉,添麻烦了。 这是完整的代码。

    var pt1 = new Array ();     //key press time
    var pr1 = new Array ();     //key press release
    var pkh1 = new Array ();    //key press horizontal component
    var pkv1 = new Array();     // key press vertical component
    var level : int = 0;
    var i : int = 0;    // i is car number of this script
    var t1 : float = 0;
    var j1 : int = 0;
    var downUp;
    var heldUp;
    var upUp;
    var downDown;
    var heldDown;
    var upDown;
    var downLeft;
    var heldLeft;
    var upLeft;
    var downRight;
    var heldRight;
    var upRight;
    var downSpace;
    var heldSpace;
    var upSpace;

    var stopwatch : System.Diagnostics.Stopwatch  = new System.Diagnostics.Stopwatch();
    var stopwatch1 : System.Diagnostics.Stopwatch  = new System.Diagnostics.Stopwatch();

    var l = true;
        var speed : float = 6f;            // The speed that the player will move at.

        private var movement : Vector3;                   // The vector to store the direction of the player's movement.
        private var playerRigidbody : Rigidbody;          // Reference to the player's rigidbody.
        function Awake () {
          playerRigidbody = GetComponent (Rigidbody);
        }
        function move (h : float, v : float)
        {
    // Set the movement vector based on the axis input.
    movement.Set (h, 0f, v);

    // Normalise the movement vector and make it proportional to the speed per second.
    movement = movement.normalized * speed * Time.deltaTime;

    // Move the player to it's current position plus the movement.
    playerRigidbody.MovePosition (transform.position + movement);
    }



    function Start () {
    stopwatch.Start();
    }

function FixedUpdate ()
{
    // Store the input axes.
    var h : float = Input.GetAxisRaw ("Horizontal");
    var v : float = Input.GetAxisRaw ("Vertical");

    // Move the player around the scene.
    move (h, v);

}

function Update () 
{
    if(l) 
    {
    //for current player car
        downUp = Input.GetKeyDown(KeyCode.UpArrow);
        heldUp = Input.GetKey(KeyCode.UpArrow);
        upUp =  Input.GetKeyUp(KeyCode.UpArrow);
        if(downUp) 
        {
            pt1[j1] = stopwatch.ElapsedMilliseconds;
            pkh1[j1] = Input.GetAxisRaw ("Horizontal");
            pkv1[j1] = Input.GetAxisRaw ("Vertical");
        }
        if(heldUp) 
        {
        }
        if(upUp) 
        {
            pr1[j1] = stopwatch.ElapsedMilliseconds;
            j1++;
        }
        downDown = Input.GetKeyDown(KeyCode.DownArrow);
        heldDown = Input.GetKey(KeyCode.DownArrow);
        upDown =  Input.GetKeyUp(KeyCode.DownArrow);
        if(downDown) {
            pt1[j1] = stopwatch.ElapsedMilliseconds;
            pkh1[j1] = Input.GetAxisRaw ("Horizontal");
            pkv1[j1] = Input.GetAxisRaw ("Vertical");
        }
        if(heldDown) {
        }
        if(upDown) {
            pr1[j1]= stopwatch.ElapsedMilliseconds;
            j1++;
        }
        downLeft = Input.GetKeyDown(KeyCode.LeftArrow);
        heldLeft = Input.GetKey(KeyCode.LeftArrow);
        upLeft =  Input.GetKeyUp(KeyCode.LeftArrow);
        if(downLeft) {
            pt1[j1] = stopwatch.ElapsedMilliseconds;
            pkh1[j1] = Input.GetAxisRaw ("Horizontal");
            pkv1[j1] = Input.GetAxisRaw ("Vertical");
        }
        if(heldLeft) {
        }
        if(upLeft) {
            pr1[j1] = stopwatch.ElapsedMilliseconds;
            j1++;
        }
        downRight = Input.GetKeyDown(KeyCode.RightArrow);
        heldRight = Input.GetKey(KeyCode.RightArrow);
        upRight =  Input.GetKeyUp(KeyCode.RightArrow);
        if(downRight) {
            pt1[j1] = stopwatch.ElapsedMilliseconds;
            pkh1[j1] = Input.GetAxisRaw ("Horizontal");
            pkv1[j1] = Input.GetAxisRaw ("Vertical");
        }
        if(heldRight) {
        }
        if(upRight) {
            pr1[j1] = stopwatch.ElapsedMilliseconds;
            j1++;
        }
        downSpace = Input.GetKeyDown(KeyCode.A);
        heldSpace = Input.GetKey(KeyCode.A);
        upSpace =  Input.GetKeyUp(KeyCode.A);
        if(downSpace || heldSpace) 
        {
            transform.position = Vector3(0f,0f,0f);
            stopwatch.Stop();
            l = false;
            stopwatch1.Start();
            j1 = 0;
        }
    }
    else {
        t1 = stopwatch1.ElapsedMilliseconds;
        Debug.Log(j1);
        Debug.Log(transform.position);
        if(pt1[j1] <= t1) {
            while(pr1[j1] >= t1) {
                move(pkh1[j1], pkv1[j1]);   
            }
            j1++;
        }
    }   
}

2 个答案:

答案 0 :(得分:0)

您是否在此等式中的任何位置使用父游戏对象?如果是这样,您可能必须使用transform.localPosition而不是transform.position

答案 1 :(得分:0)

使用游戏对象定义路径或路线可能更容易,只需让脚本告诉玩家移动到当前目标,您可以将其设置为课程中的下一个对象。

您可以直观地展示路径,并且在我们的课程中移动点的代码将变得简单并且错误的空间也会减少。

有些事情:

定义1-x

课程的游戏对象列表

玩家将当前目标设为1

玩家移动到当前目标

当玩家达到当前目标时(您可以通过碰撞检测来管理此目标)增加当前目标+1以开始移动到下一个目标。

只是一个想法。