using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public List<Vector3> points = new List<Vector3>();
// Start is called before the first frame update
void Start()
{
points.Add(transform.position);
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < points.Count; i++)
{
transform.position = Vector3.Lerp(points[i], pos2, (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f);
}
}
}
第一个航点是变换(移动物体)的位置,所以当游戏运行时,变换会移动到第一个航点,如果需要的话,它会先面向第一个航点缓慢旋转,然后再移动到该航点。当变换物到达下一个(第一个)航点时,它将面向下一个航点缓慢旋转,同时移动到下一个航点。以此类推,直到航点结束;然后再从下一轮开始,第一个航点将是列表中不转移原始位置的第一个第二点。原来的位置只在第一轮中作为航点使用。
我试过这样做,但是对象ToPatrol的移动是结结巴巴的,不是直接移动到第一个航点,而是移动到第一个航点,但也是向上移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public Transform[] waypoints;
public float waypointRadius = 1.5f;
public float damping = 0.1f;
public bool loop = false;
public float speed = 2.0f;
public bool faceHeading = true;
public GameObject objectToPatrol;
private Vector3 currentHeading, targetHeading;
private int targetwaypoint;
private Transform xform;
private bool useRigidbody;
private Rigidbody rigidmember;
// Use this for initialization
protected void Start()
{
if (objectToPatrol == null)
{
xform = transform;
}
else
{
xform = objectToPatrol.transform;
}
currentHeading = xform.forward;
if (waypoints.Length <= 0)
{
Debug.Log("No waypoints on " + name);
enabled = false;
}
targetwaypoint = 0;
if (GetComponent<Rigidbody>() != null)
{
useRigidbody = true;
rigidmember = GetComponent<Rigidbody>();
}
else
{
useRigidbody = false;
}
}
// calculates a new heading
protected void FixedUpdate()
{
targetHeading = waypoints[targetwaypoint].position - xform.position;
currentHeading = Vector3.Lerp(currentHeading, targetHeading, damping * Time.deltaTime);
}
// moves us along current heading
protected void Update()
{
if (useRigidbody)
rigidmember.velocity = currentHeading * speed;
else
xform.position += currentHeading * Time.deltaTime * speed;
if (faceHeading)
xform.LookAt(xform.position + currentHeading);
if (Vector3.Distance(xform.position, waypoints[targetwaypoint].position) <= waypointRadius)
{
targetwaypoint++;
if (targetwaypoint >= waypoints.Length)
{
targetwaypoint = 0;
if (!loop)
enabled = false;
}
}
}
// draws red line from waypoint to waypoint
public void OnDrawGizmos()
{
Gizmos.color = Color.red;
if (waypoints == null)
return;
for (int i = 0; i < waypoints.Length; i++)
{
Vector3 pos = waypoints[i].position;
if (i > 0)
{
Vector3 prev = waypoints[i - 1].position;
Gizmos.DrawLine(prev, pos);
}
}
}
}
第一个问题是,我想在航点之间移动的对象有一个Animator组件,而Animator使它结结巴巴。有什么方法可以让Animator保持启用并平滑地移动它,我的意思是,它将继续播放对象动画,在这种情况下,闲置但也平滑地移动它?
你可以尝试使用 Vector3.MoveTowards 而不是 targetHeading
和 currentHeading
,像这样。
var newPosition = Vector3.MoveTowards(transform.Position, waypoints[targetwaypoint].position, Time.deltaTime * speed);
if (useRigidbody)
rigidmember.MovePosition(newPosition);
else
xform.position.position = newPosition;
至于第二个问题,你可以尝试把动画游戏对象 放在一个空的游戏对象中,然后把脚本放在父对象上。
链接 https:/docs.unity3d.comScriptReferenceRigidbody.MovePosition.html。 https:/answers.unity.comquestions199744how-would-i-move-a-rigidbody-towards-another-objec.html。 https:/docs.unity3d.comScriptReferenceVector3.MoveTowards.html。