概述
跟随路径点移动:
public Transform[] ways1;//路径点的位置
public Transform[] ways;
public Transform tagret;//移动的物体
public Transform Player;
public float ZhuJueSpeed;
private int index1, index;
/// <summary>
/// 玩家移动
/// </summary>
public void Move() {
if (index > ways.Length - 1) { return; }
Player.localPosition = Vector3.MoveTowards(Player.localPosition, ways[index].localPosition, ZhuJueSpeed * Time.deltaTime);
//Player.transform.LookAt(ways[index]);
Vector3 v0 = Player.localEulerAngles;
Player.transform.LookAt(ways1[index]);
Vector3 v1 = Player.localEulerAngles;
v0 = Vector3.Lerp(v0, v1, 0.1f);
Player.localEulerAngles = v0;
//Player.position = Vector3.Lerp(Player.position, ways[index].position, 0.03f * Time.deltaTime);
if (Vector3.Distance(ways[index].localPosition, Player.localPosition) < 0.01f) {
index++;
if (index == ways.Length) {
Player.localPosition = ways[index - 1].localPosition;
}
}
}
/// <summary>
/// 前方车辆移动
/// </summary>
public void Move1() {
if (index1 > ways1.Length - 1) { return; }
tagret.localPosition = Vector3.MoveTowards(tagret.localPosition, ways1[index1].localPosition, speed * Time.deltaTime);
//tagret.transform.eulerAngles = new Vector3(ways1[index1].position.x, ways1[index1].position.y);
Vector3 v0 = tagret.localEulerAngles;
tagret.transform.LookAt(ways1[index1]);
Vector3 v1 = tagret.localEulerAngles;
v0 = Vector3.Lerp(v0, v1, 0.2f);
tagret.localEulerAngles = v0;
//tagret.transform.rotation = Quaternion.Lerp(tagret.transform.rotation, Quaternion.LookRotation(ways1[index1].position- tagret.position),0.1f);
//tagret.position=Vector3.Lerp(tagret.position, ways1[index1].position,0.06f*Time.deltaTime);
//tagret.position = Vector3.Lerp(ways1[index1].position, ways1[index1+1].position,Vector3.Distance(tagret.position, ways1[index1].position)/Vector3.Distance(ways1[index1 + 1].position, ways1[index1].position)*Time.deltaTime*0.01f);
if (Vector3.Distance(ways1[index1].localPosition, tagret.localPosition) < 0.01f) {
index1++;
if (index1 == ways1.Length) {
tagret.localPosition = ways1[index1 - 1].localPosition;
}
}
}
往返移动:
public class posMove : MonoBehaviour {
public float speed;
//public AnimationCurve curve;
float scales;
private Vector3 StartPosition;
private Vector3 EndPosition;
private bool isArrive { get; set; } = false;
private void Start() {
speed = Random.Range(0.05f, 0.1f);
scales = Random.Range(0.05f, 0.15f);
transform.localScale = new Vector3(scales, scales, scales);
StartPosition = transform.localPosition;
float z = transform.localPosition.z + 0.2f;
EndPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, z);
}
private void FixedUpdate() {
float step = speed * Time.fixedDeltaTime;
if (!isArrive)
transform.position = Vector3.MoveTowards(transform.localPosition, EndPosition, step);
else
transform.position = Vector3.MoveTowards(transform.localPosition, StartPosition, step);
if (this.transform.localPosition.x == EndPosition.x && this.transform.localPosition.z == EndPosition.z && isArrive == false) {
isArrive = true;
}
else if (this.transform.localPosition.x == StartPosition.x && this.transform.localPosition.z == StartPosition.z && isArrive == true) {
isArrive = false;
}
}
}
最后
以上就是苹果战斗机为你收集整理的Unity 车辆跟随路径点缓动移动的全部内容,希望文章能够帮你解决Unity 车辆跟随路径点缓动移动所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复