如何顺利变道



我正在努力实现基本的赛车游戏。无限竞速游戏,运动方式犹如地铁冲浪者。我对变道有意见。我不想传送到另一条车道,我想顺利。我是一个团结的新手,我尝试过勒普方法,但它不起作用。

using UnityEngine;
using System.Collections;
public class VehicleController : MonoBehaviour 
{
    public float drift;
    public Vector3 positionA;
    public Vector3 positionB;
    public Vector3 positionC;
    public Vector3 positionD;
    private Transform tf;
    private Rigidbody rb;
    private Vector3 vehiclePos;
    void Awake()
    {
        //rb = GetComponent<Rigidbody> ();
        tf = transform;
    }
    void Update()
    {
        vehiclePos = tf.position;
        if (Input.GetKey( KeyCode.Space ))
            DecreaseSpeed ();
        else
            IncreaseSpeed ();
        if (Input.GetKeyDown (KeyCode.A)) 
        {
            MoveToRight ();
            Debug.Log( "Move to Right!" );
        }
        if (Input.GetKeyDown (KeyCode.D)) 
        {
            MoveToLeft ();
            Debug.Log( "Move to Left!" );
        }
    }
    void FixedUpdate()
    {
        tf.Translate (Vector3.forward * speed * Time.deltaTime);//My Movement Method.
    }
    void MoveToLeft()
    {
        if (vehiclePos.position.x == positionA.x)
            vehiclePos = Vector3.Lerp (vehiclePos.position, positionB, Time.deltaTime * drift);
    }
    void MoveToRight()
    {
        if (vehiclePos.position.x == positionB.x)
            vehiclePos = Vector3.Lerp (vehiclePos.position, positionA, Time.deltaTime * drift);
    }
}

第一:不要对position.x使用==,因为它是一个浮点(小数)值,在这种情况下,它很少返回"true"。以下是一些关于比较浮动的信息。

第二:看起来你并没有把你的实际位置和任何地方的vehiclePos联系起来。transform.position就是你想要的。

第三:Input.GetAxis()是处理方向输入的一种更干净的方法。您可以只处理一个介于-1和1之间的浮点值,而不是专门调用每个按钮。它还可以让您轻松地重新配置密钥。

第四:在一个无限的奔跑者中,让世界朝着你的角色和相机移动比让角色和相机真正向前移动要好。随着你离零越来越远,浮点数字的精度会越来越低,所以如果可以的话,你应该让你的动作相对靠近世界原点(0,0,0)点。

如果你想按一次按钮改变车道,你应该保留一个整数变量,保存你当前所在的车道。如果你按LEFT,你会减一,如果你按RIGHT,你会加一。您还应该添加一个检查,以确保它保持在所需的范围内。

然后在Update()中,您只需要始终向X值倾斜即可。如果需要,可以使用Mathf.Lerp一次只提示一个变量。

public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Update() {
    //"Horizontal" is a default input axis set to arrow keys and A/D
    //We want to check whether it is less than the deadZone instead of whether it's equal to zero 
    float input = Input.GetAxis("Horizontal");
    if(Mathf.Abs(input) > deadZone) {
        if(!didChangeLastFrame) {
            didChangeLastFrame = true; //Prevent overshooting lanes
            laneNumber += Mathf.roundToInt(Mathf.Sign(input));
            if(laneNumber < 0) laneNumber = 0;
            else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
        }
    } else {
        didChangeLastFrame = false;
        //The user hasn't pressed a direction this frame, so allow changing directions next frame.
    }
    Vector3 pos = transform.position;
    pos.x = Mathf.Lerp(pos.x, firstLandXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
    transform.position = pos;
}

您可能只是按原样使用此代码,但我建议您仔细查看并尝试了解它的工作方式和原因。今天,一个总是寻求提高技能的新手下周可以做一些了不起的事情。希望这能有所帮助。:)

最新更新