1
경로를 따라 차량을 회전해야하지만 코드가 올바르게 작동하지 않습니다. 불꽃처럼 움직입니다. 문제는 z 축에서 x 축으로 이동하는 것입니다. 나는이 문제를 해결할 충분한 지식이 없다. 도움!단일체로 움직이는 동안 물체를 회전시키는 방법은 무엇입니까?
내가 언젠가 전에이 코드를 사용using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public Transform StartPoint;
public Transform[] EndPoints;
public Transform[] AngleWaypoints;
public float MoveSpeed;
private Transform TargetPoint;
//private Transform EndPoint;
private float DistanceToPoint = 0f;
private int RandomValue;
private Transform[] Waypoints;
private int wpIndex = 0;
private Quaternion lookRotation;
private Vector3 direction;
// Use this for initialization
void Start()
{
RandomValue = Random.Range (1, EndPoints.Length);
int count = 0;
switch (RandomValue) {
case 0:
//Waypoints = StartPoint + EndPoints [RandomValue];
count = 2;
Waypoints = new Transform[count];
Waypoints [0] = StartPoint;
Waypoints [1] = EndPoints[RandomValue];
break;
case 1:
//Waypoints = StartPoint + AngleWaypoints + EndPoints[RandomValue];
count = 2 + AngleWaypoints.Length;
Waypoints = new Transform[count];
Waypoints [0] = StartPoint;
for(int i=0;i<AngleWaypoints.Length;i++){
Waypoints [i + 1] = AngleWaypoints [i];
}
Waypoints [count-1] = EndPoints[RandomValue];
break;
}
TargetPoint = Waypoints [wpIndex];
}
// Update is called once per frame
void Update()
{
DistanceToPoint = Vector3.Distance (transform.position, TargetPoint.position);
if (DistanceToPoint > 1) {
direction = (TargetPoint.position - transform.position)/2 + transform.position;
Vector3 dir = TargetPoint.position - transform.position;
lookRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime);
Vector3 v = direction/DistanceToPoint * MoveSpeed * Time.deltaTime;
transform.Translate (v);
} else {
wpIndex++;
if (wpIndex > Waypoints.Length - 1) {
wpIndex = 0;
Destroy(this.gameObject);
}
TargetPoint = Waypoints[wpIndex];
}
}
}
멋진 일! 고맙습니다 –