【发布时间】:2016-07-27 18:01:12
【问题描述】:
我猜问题出在某处的航点脚本中。
如果我让玩家走慢,比如说速度 1 或 2,他会走到第一个航路点,然后当他到达第二个航路点时转向下一个航路点,而不是回到原来的位置,他会绕圈走,然后走回第一个航路点。我想要它做的不是走到第一个航路点,而是走到它原来的起始位置。
另一件事,如果我将玩家速度更改为 7,当他到达第二个航路点时,我看到它走得很快,他左转并不停地向左走。就像根据玩家的速度他的表现不同。
这是一个视频剪辑,展示了速度为 2 的播放器: 第二个走得更快的玩家在速度 2 上,看看当他到达第二个路点时会发生什么:
这是速度 7 的第二个玩家
这是 c# 中的航点脚本
using UnityEngine;
using System.Collections;
public class Waypoints : MonoBehaviour {
public Transform[] waypoint;
public float patrolSpeed;
public bool loop = true;
public int dampingLook = 4;
public float pauseDuration;
private float curTime;
private int currentWaypoint = 0;
public CharacterController character;
// Use this for initialization
void Start () {
}
void LateUpdate(){
if(currentWaypoint < waypoint.Length){
patrol();
}else{
if(loop){
currentWaypoint=0;
}
}
}
void patrol(){
Vector3 nextWayPoint = waypoint[currentWaypoint].position;
// Keep waypoint at character's height
nextWayPoint.y = transform.position.y;
// Get the direction we need to move to
// reach the next waypoint
Vector3 moveDirection = nextWayPoint - transform.position;
if(moveDirection.magnitude < 1.5){
Debug.Log("enemy is close to nextwaypoint");
// This section of code is called only whenever the enemy
// is very close to the new waypoint
// so it is called once after 4-5 seconds.
if (curTime == 0)
// Pause over the Waypoint
curTime = Time.time;
if ((Time.time - curTime) >= pauseDuration){
Debug.Log("increasing waypoint");
currentWaypoint++;
curTime = 0;
}
}
else
{
Debug.Log("reaching in rotation " + moveDirection.magnitude);
// This code gets called every time update is called
// while the enemy if moving from point 1 to point 2.
// so it gets called 100's of times in a few seconds
// Now we need to do two things
// 1) Start rotating in the desired direction
// 2) Start moving in the desired direction
// 1) Let' calculate rotation need to look at waypoint
// by simply comparing the desired waypoint & current transform
var rotation = Quaternion.LookRotation(nextWayPoint - transform.position);
// A slerp function allow us to slowly start rotating
// towards our next waypoint
transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
Time.deltaTime * dampingLook);
// 2) Now also let's start moving towards our waypoint
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
}
也许玩家的这种奇怪行为在物理上是可以的?那么如果它太快了,它会产生这种行为吗?但这很奇怪。我认为无论在航路点之间行走的速度如何,都不是?
【问题讨论】:
标签: c# unity3d unityscript unity5