我刚开始接触unity。代码的目的是使游戏对象回溯一条路径,我正在尝试编写一段代码来保存玩家控制的游戏对象的路径,当按下'A‘时,游戏对象被重新定位到起点,然后按照保存的路径移动。
代码能够保存输入(使用Debug.log检查),它还可以移动到起始位置。但不会根据保存的路径移动。
游戏对象总是从(0,0,0)移动到(-0.7,0,0),而不考虑输入(即使我没有输入,直接按'A')。
我可能犯了一个愚蠢的错误。很抱歉给您添了麻烦。下面是完整的代码。
    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++;
        }
    }   
}发布于 2016-04-08 13:44:30
你在这个方程式中的任何地方都在使用父游戏对象吗?如果是这样,您可能必须使用transform.localPosition而不是transform.position
发布于 2016-04-08 22:08:52
使用游戏对象来定义路径或路线可能更容易,只需让脚本告诉玩家朝当前目标移动,您可以将其设置为课程中的下一个对象。
你可以在视觉上铺设路径,在铺设好的道路上移动到点的代码我们的课程将会很简单,错误的空间会更小。
大致是这样的:
定义属于课程1-x的游戏对象列表
玩家将当前目标设置为1
玩家移动到当前目标
当玩家到达当前目标时(你可以通过碰撞检测来管理),增加当前目标+1开始移动到下一个目标。
这只是个想法。
https://stackoverflow.com/questions/36492320
复制相似问题