Unity3d 回溯路径有问题

Unity3d 回溯路径有问题,unity3d,unityscript,Unity3d,Unityscript,我不熟悉团结。代码的目的是使游戏对象回溯路径。我试图制作一个代码,保存玩家控制的游戏对象的路径,按“a”键时,游戏对象被重新定位到起点,然后根据保存的路径移动 代码可以保存输入(使用Debug.log进行检查),它也可以移动到staarting位置。但不会根据保存的路径移动 游戏对象总是从(0,0,0)移动到(-0.7,0,0),而不考虑输入(即使我没有输入&直接按“A”) 我可能犯了一个愚蠢的错误。抱歉给你添麻烦了。 这是完整的代码 var pt1 = new Array ();

我不熟悉团结。代码的目的是使游戏对象回溯路径。我试图制作一个代码,保存玩家控制的游戏对象的路径,按“a”键时,游戏对象被重新定位到起点,然后根据保存的路径移动

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

游戏对象总是从(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++;
        }
    }   
}
var pt1=新数组()//按键时间
var pr1=新数组()//关键新闻稿
var pkh1=新数组()//按键水平组件
var pkv1=新数组();//按键垂直分量
变量级别:int=0;
变量i:int=0;//我是这个剧本的车号
变量t1:float=0;
变量j1:int=0;
var下降;
var heldUp;
var-upUp;
var下降;
瓦尔赫德敦;
var向上向下;
左下角;
瓦尔赫德尔夫特;
var-upLeft;
绝对值;
瓦尔赫德里特;
垂直变异;
var下降空间;
var-heldSpace;
var上升空间;
var stopwatch:System.Diagnostics.stopwatch=new System.Diagnostics.stopwatch();
var stopwatch1:System.Diagnostics.Stopwatch=new System.Diagnostics.Stopwatch();
var l=真;
var速度:浮动=6f;//玩家移动的速度。
私有变量移动:Vector3;//用于存储玩家移动方向的向量。
private var playerRigidbody:刚体;//指运动员的刚体。
函数唤醒(){
playerRigidbody=GetComponent(刚体);
}
功能移动(h:浮动,v:浮动)
{
//根据轴输入设置运动矢量。
运动设定(h、0f、v);
//将运动矢量归一化,使其与每秒速度成比例。
移动=移动。标准化*速度*时间。延迟时间;
//将玩家移动到当前位置加上移动。
playerRigidbody.MovePosition(transform.position+movement);
}
函数启动(){
秒表。开始();
}
函数FixedUpdate()
{
//存储输入轴。
var h:float=Input.GetAxisRaw(“水平”);
变量v:float=Input.GetAxisRaw(“垂直”);
//在场景中移动播放器。
移动(h,v);
}
函数更新()
{
如果(l)
{
//对于当前的玩家汽车
downUp=Input.GetKeyDown(KeyCode.UpArrow);
heldUp=Input.GetKey(KeyCode.UpArrow);
upUp=Input.GetKeyUp(KeyCode.UpArrow);
如果(向下)
{
pt1[j1]=秒表。延时毫秒;
pkh1[j1]=Input.GetAxisRaw(“水平”);
pkv1[j1]=Input.GetAxisRaw(“垂直”);
}
如果(heldUp)
{
}
如果(向上)
{
pr1[j1]=秒表。延时毫秒;
j1++;
}
downDown=Input.GetKeyDown(KeyCode.DownArrow);
heldDown=Input.GetKey(KeyCode.DownArrow);
upDown=Input.GetKeyUp(KeyCode.DownArrow);
如果(向下){
pt1[j1]=秒表。延时毫秒;
pkh1[j1]=Input.GetAxisRaw(“水平”);
pkv1[j1]=Input.GetAxisRaw(“垂直”);
}
如果(直升机降落){
}
如果(向上向下){
pr1[j1]=秒表。延时毫秒;
j1++;
}
downlight=Input.GetKeyDown(KeyCode.LeftArrow);
heldLeft=Input.GetKey(KeyCode.LeftArrow);
upLeft=Input.GetKeyUp(KeyCode.LeftArrow);
如果(左下){
pt1[j1]=秒表。延时毫秒;
pkh1[j1]=Input.GetAxisRaw(“水平”);
pkv1[j1]=Input.GetAxisRaw(“垂直”);
}
如果(heldLeft){
}
如果(英尺){
pr1[j1]=秒表。延时毫秒;
j1++;
}
downRight=Input.GetKeyDown(KeyCode.RightArrow);
heldRight=Input.GetKey(KeyCode.RightArrow);
直立=Input.GetKeyUp(KeyCode.RightArrow);
如果(完全正确){
pt1[j1]=秒表。延时毫秒;
pkh1[j1]=Input.GetAxisRaw(“水平”);
pkv1[j1]=Input.GetAxisRaw(“垂直”);
}
if(直升机右舵){
}
如果(直立){
pr1[j1]=秒表。延时毫秒;
j1++;
}
downSpace=Input.GetKeyDown(KeyCode.A);
heldSpace=Input.GetKey(KeyCode.A);
upSpace=Input.GetKeyUp(KeyCode.A);
if(下行空间| |直升机空间)
{
transform.position=Vector3(0f,0f,0f);
秒表;
l=假;
stopwatch1.Start();
j1=0;
}
}
否则{
t1=秒表1.0毫秒;
Debug.Log(j1);
Debug.Log(transform.position);
如果(pt1[j1]=t1){
移动(pkh1[j1],pkv1[j1]);
}
j1++;
}
}   
}

您是否在该等式中的任何位置使用父游戏对象?如果是这样,您可能必须使用transform.localPosition而不是transform.position,使用游戏对象来定义路径或路线可能更容易,只需让脚本告诉玩家向当前目标移动,您可以将其设置为路线中的下一个对象

您可以直观地布局路径,并且在布局过程中向点移动的代码将非常简单,并且出错的空间更小

大致如下:

定义球场1-x的游戏对象列表

玩家将当前目标设置为1

玩家移动到当前目标

当玩家到达当前目标时(你可以通过碰撞来控制)