如何顺利变道
How to change lane smoothly
我正在尝试实现基本的赛车游戏。无限赛车游戏,像地铁冲浪者一样的运动方式。我有一个关于改变车道的问题。我不想传送到其他车道,我想顺利。我是统一的新手,我尝试过 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);
}
}
首先:不要对 position.x 使用 ==,因为它是一个浮点(小数)值,在这种情况下,return [=28] 是非常罕见的=]. Here's some info about comparing floats.
其次:您似乎没有在任何地方将您的实际位置与 vehiclePos 连接起来。 transform.position
就是你想要的。
第三:Input.GetAxis() 是一种更简洁的处理方向输入的方法。您可以只处理一个介于 -1 和 1 之间的浮点值,而不是专门调用每个按钮。它还可以让您轻松地重新配置键。
第四:在无限奔跑中,让世界朝着你的角色和相机移动比让角色和相机实际向前移动要好。当您远离零时,浮点数会变得不太精确,因此如果可以的话,您应该让您的操作相对靠近世界原点 (0,0,0) 点。
如果你想按一次按钮来改变车道,你应该保留一个整数变量来保存你当前所在的车道。如果你按左,你减一,如果你按右,你加一。您还应该添加一个检查以确保它保持在所需范围内。
然后在 Update() 中,您只需要始终 Lerp 到该 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;
}
您可能只按原样使用此代码,但我建议您仔细阅读并尝试了解它的工作原理和原因。今天总是寻求提高技能的新手下周可以做出惊人的事情。希望这可以帮助。 :)
我正在尝试实现基本的赛车游戏。无限赛车游戏,像地铁冲浪者一样的运动方式。我有一个关于改变车道的问题。我不想传送到其他车道,我想顺利。我是统一的新手,我尝试过 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);
}
}
首先:不要对 position.x 使用 ==,因为它是一个浮点(小数)值,在这种情况下,return [=28] 是非常罕见的=]. Here's some info about comparing floats.
其次:您似乎没有在任何地方将您的实际位置与 vehiclePos 连接起来。 transform.position
就是你想要的。
第三:Input.GetAxis() 是一种更简洁的处理方向输入的方法。您可以只处理一个介于 -1 和 1 之间的浮点值,而不是专门调用每个按钮。它还可以让您轻松地重新配置键。
第四:在无限奔跑中,让世界朝着你的角色和相机移动比让角色和相机实际向前移动要好。当您远离零时,浮点数会变得不太精确,因此如果可以的话,您应该让您的操作相对靠近世界原点 (0,0,0) 点。
如果你想按一次按钮来改变车道,你应该保留一个整数变量来保存你当前所在的车道。如果你按左,你减一,如果你按右,你加一。您还应该添加一个检查以确保它保持在所需范围内。
然后在 Update() 中,您只需要始终 Lerp 到该 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;
}
您可能只按原样使用此代码,但我建议您仔细阅读并尝试了解它的工作原理和原因。今天总是寻求提高技能的新手下周可以做出惊人的事情。希望这可以帮助。 :)