【问题标题】:How to change lane smoothly如何顺利变道
【发布时间】:2015-03-21 16:18:33
【问题描述】:

我正在尝试实现基本的赛车游戏。无限的赛车游戏,像地铁冲浪者一样的运动方法。我有变道的问题。我不想传送到其他车道,我想顺利。我是团结的新手,我尝试过 Lerp 方法,但它不起作用。

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);
    }
}

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    首先:不要对 position.x 使用 ==,因为它是一个浮点(十进制)值,在这种情况下,它很少返回“true”。 Here's some info about comparing floats.

    第二:看起来您并没有在任何地方将您的实际位置与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;
    }
    

    您可能只是按原样使用此代码,但我建议您仔细查看并尝试弄清楚它的工作原理和原因。今天的新手总是寻求提高他们的技能,下周可以做一些惊人的事情。希望这可以帮助。 :)

    【讨论】:

    • 如何通过触摸滑动进行控制(仅 x 轴、左右)。滑动值也在 (1, -1) 之间吗?
    猜你喜欢
    • 2016-02-17
    • 2016-10-10
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2010-11-22
    • 2016-09-27
    相关资源
    最近更新 更多