首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有关于回溯路径的问题

有关于回溯路径的问题
EN

Stack Overflow用户
提问于 2016-04-08 13:39:27
回答 2查看 81关注 0票数 0

我刚开始接触unity。代码的目的是使游戏对象回溯一条路径,我正在尝试编写一段代码来保存玩家控制的游戏对象的路径,当按下'A‘时,游戏对象被重新定位到起点,然后按照保存的路径移动。

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

游戏对象总是从(0,0,0)移动到(-0.7,0,0),而不考虑输入(即使我没有输入,直接按'A')。

我可能犯了一个愚蠢的错误。很抱歉给您添了麻烦。下面是完整的代码。

代码语言:javascript
运行
复制
    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++;
        }
    }   
}
EN

Stack Overflow用户

发布于 2016-04-08 13:44:30

你在这个方程式中的任何地方都在使用父游戏对象吗?如果是这样,您可能必须使用transform.localPosition而不是transform.position

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36492320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档