C# 我如何在一组航路点之间移动一个对象,并在每个航路点面对下一个航路点进行平滑缓慢旋转? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类航路点:单一行为 { 公共列表点=新列表(); //在第一帧更新之前调用Start void Start() { 点。添加(变换。位置); } //每帧调用一次更新 无效更新() { 对于(int i=0;i
第一个航路点是变换(移动对象)位置,因此在运行游戏时,变换将移动到第一个航路点,如果需要,它将首先面向第一个航路点缓慢旋转,然后移动到第一个航路点。当变换到达下一个(第一个)航路点时,它将朝着下一个航路点缓慢旋转,同时移动到下一个航路点。以此类推,直到航路点结束;然后,从下一轮开始,第一个航路点将是列表中未转移原始位置的第一个第二个点。原始位置仅在第一轮中用作航路点 我试过了,但是objectToPatrol移动时口吃,不是直接移动到第一个航路点,而是移动到第一个航路点,但也向上移动C# 我如何在一组航路点之间移动一个对象,并在每个航路点面对下一个航路点进行平滑缓慢旋转? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类航路点:单一行为 { 公共列表点=新列表(); //在第一帧更新之前调用Start void Start() { 点。添加(变换。位置); } //每帧调用一次更新 无效更新() { 对于(int i=0;i,c#,unity3d,C#,Unity3d,第一个航路点是变换(移动对象)位置,因此在运行游戏时,变换将移动到第一个航路点,如果需要,它将首先面向第一个航路点缓慢旋转,然后移动到第一个航路点。当变换到达下一个(第一个)航路点时,它将朝着下一个航路点缓慢旋转,同时移动到下一个航路点。以此类推,直到航路点结束;然后,从下一轮开始,第一个航路点将是列表中未转移原始位置的第一个第二个点。原始位置仅在第一轮中用作航路点 我试过了,但是objectToPatrol移动时口吃,不是直接移动到第一个航路点,而是移动到第一个航路点,但也向上移动 using
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);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类航路点:单一行为
{
公共转换[]航路点;
公共浮动航路点半径=1.5f;
公共浮子阻尼=0.1f;
公共布尔循环=假;
公共浮子速度=2.0f;
公共bool faceHeading=true;
公共游戏对象objectToPatrol;
专用矢量3当前航向,目标航向;
专用int targetwaypoint;
私有变换变换;
私有布尔用户实体;
私人刚体刚体成员;
//用于初始化
受保护的void Start()
{
if(objectToPatrol==null)
{
变换=变换;
}
其他的
{
xform=objectToPatrol.transform;
}
currentHeading=xform.forward;
if(航路点长度0)
{
Vector3 prev=航路点[i-1]。位置;
Gizmos.抽绳(前、后);
}
}
}
}
第一个问题是,我想在航路点之间移动的对象有一个动画组件,而动画制作者使它口吃。有没有办法让动画师保持启用状态并使其平滑移动?我的意思是,在这种情况下,动画师将保持播放对象动画的空闲状态,但也会使其平滑移动
您可以尝试使用而不是目标阅读
和当前标题
,如下所示:
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);
}
}
}
}
至于第二个问题,您可以尝试将动画游戏对象放在空游戏对象中,然后将脚本放在父对象上
链接:请考虑在你的文章中编辑这一部分:“第一个航路点将是第一个第二个点”;我认为,“第一秒”的位置可能会让你问题的读者感到困惑。
var newPosition = Vector3.MoveTowards(transform.Position, waypoints[targetwaypoint].position, Time.deltaTime * speed);
if (useRigidbody)
rigidmember.MovePosition(newPosition);
else
xform.position.position = newPosition;