我已经尝试了一段时间,并且无法弄清楚它为什么不起作用。 我在PathHolder父级中有一个简单的空对象路径,并且跟随该路径的一个字符对象可以平滑地旋转以面向它要朝向的点。我的问题是,有一半的时间,角色对象不会一直旋转,但仍然朝着这个点移动。 另一个奇怪的事情是,将点移近会减少物体旋转的程度,而进一步移动将实际上使角色面向点。但是,路径需要灵活,以便在任何情况下工作,所以我不能让距离影响旋转。
理想情况下,当按下箭头键时,我希望角色完全平滑地旋转到该点,这样如果玩家释放按键,角色就不会卡在中间位置。这是我到目前为止的代码:
using UnityEngine;
using System.Collections;
public class CharacterControllerPath : MonoBehaviour
{
enum Direction {Forward, Backward};
Direction charDirection;
public PathEditor pathToFollow;
private Vector3 targetPosition;
public string pathName;
public int wayPointID = 0;
public float speed;
public float rotationSpeed = 7.0f;
private float reachDistance = 1.0f;
private float distanceX;
private float distanceY;
void Start ()
{
transform.position = new Vector3(pathToFollow.pathObj[wayPointID].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID].transform.position.z);
charDirection = Direction.Forward;
Vector3 targetPosition = new Vector3(pathToFollow.pathObj[wayPointID + 1].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID + 1].transform.position.z);
transform.LookAt(targetPosition);
}
void FixedUpdate ()
{
Movement();
}
void Movement()
{
if(Input.GetKey(KeyCode.RightArrow))
{
charDirection = Direction.Forward;
//Move
targetPosition = new Vector3(pathToFollow.pathObj[wayPointID].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID].transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
//Rotate
targetPosition = new Vector3(pathToFollow.pathObj[wayPointID].transform.position.x, 0, pathToFollow.pathObj[wayPointID].transform.position.z);
var rotation = Quaternion.LookRotation(targetPosition);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
if(transform.position.x == targetPosition.x && transform.position.z == targetPosition.z)
wayPointID++;
}
else if(Input.GetKey(KeyCode.LeftArrow))
{
charDirection = Direction.Backward;
//Move
targetPosition = new Vector3(pathToFollow.pathObj[wayPointID - 1].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID - 1].transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
//Rotate
targetPosition = new Vector3(pathToFollow.pathObj[wayPointID - 1].transform.position.x, 0, pathToFollow.pathObj[wayPointID - 1].transform.position.z);
var rotation = Quaternion.LookRotation(targetPosition);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
if(transform.position.x == targetPosition.x && transform.position.z == targetPosition.z)
wayPointID--;
}
if(wayPointID >= pathToFollow.pathObj.Count)
{
//Put code to finish the level
wayPointID = (pathToFollow.pathObj.Count - 1);
}
else if(wayPointID <= 0)
wayPointID = 0;
}
}
答案 0 :(得分:0)
如果要在两个向量之间平滑移动Vector,请使用
Vector3.Lerp(firstPosOrRotation, secondPosOrRotation, Time.deltaTime*Input.GetInputRaw("Horizontal")*speed);
Input.GetInputRaw(“Horizontal”)只是介于-1和1之间的值,具体取决于按下的箭头键。 现在只需获取路径中的每个点和下一个点以及它们之间的Lerp。
编辑 对于旋转,而不是Time.deltaTime和所有这些,执行此操作
rotation = Vector3.Lerp(rotationA,rotationB,Mathf.Clamp(dist(pos,pos, finalPos),0,1);
DIST(POS, finalPos)=在A点和B点之间行驶的数量
我希望这很清楚, 我不确定你的意思,或者你希望对象在路径上面向前方。