unity中,想要实现车辆追随前方设定的路径点,但是在经过路径点的时候,车头会向下倾斜,是什么原因导致的
using UnityEngine;
using static System.Net.WebRequestMethods;
public class egoAV_new : MonoBehaviour//源脚本
{
public Transform[] waypoints;// 空物体数组,作为路径点
private int currentWaypointIndex; // 当前路径点的索引
public float current_speed;//在防御型ego-AV中引入了减速度,所以当前车辆会发生车速变化。
public bool isAV = true;//标志,用于控制自动驾驶
public MonoBehaviour logic;
private void Start()
{
current_speed = 30 / 3.6f;
currentWaypointIndex = 0;
}
private void Update()
{
//rec中有fxp,youmeng,shache3个float值
LogitechGSDK.DIJOYSTATE2ENGINES rec;
//持续让fxp,youmeng,shache这3个值归0,即恢复到初始位置
rec = LogitechGSDK.LogiGetStateUnity(0);
float fxp = rec.lX / 32767f; //方向盘归一化(-1,1) ps:罗技G29的角度变化最大范围为900°
float youmeng = -((rec.lY + 32767f) / 65534f) + 1;//(0,1)
float shache = -((rec.lRz + 32767f) / 65534f) + 1;//(0,1)
if (isAV)
{
AVmove();
}
//从自动向手动的控制(用罗技控制)
//if (/*this.transform.position.z >= s*&& (*/ fxp >= 0.5 || fxp <= -0.5 || youmeng >= 0.05 || shache >= 0.05)
// {
// //takeover = 1;
// //avoffdisplay = true; //显示自动驾驶模式的HUD消失
// //avondisplay = false;
// isAV = false;//禁用自动驾驶脚本
// logic.enabled = true;//开始启用手动控制脚本
// }
}
void AVmove()
{
//防御型AV中的独有的减速脚本
/*if (isDefensive && transform.position.z >= s)
{
//Debug.Log("comein");
isspeedup = false;
current_speed += deceleration * Time.deltaTime;
// 确保速度不会小于2
if (current_speed < 2.5)
{
current_speed = 2;
}
}*/
Debug.Log("执行");
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypointIndex].position, current_speed * Time.deltaTime);
if (Vector3.Distance(transform.position, waypoints[currentWaypointIndex].position) < 0.1f)
{
currentWaypointIndex++;
if (currentWaypointIndex >= waypoints.Length)
{
isAV = false;
}
}
Quaternion lookRotation = Quaternion.LookRotation(waypoints[currentWaypointIndex].position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
}
