为什么我的飞机只在中途移动而不是一直移动?
Why is my airplane moving only partway and not all the way?
所以我花了 2 天时间查看这个脚本,但我仍然看不出我做错了什么。我想制作一架简单的 AI 飞机,它可以从一个点飞到另一个点。到达航点 1 后,它应该飞向航点 2。我将在下面包含我的脚本:
using UnityEngine;
using System.Collections
public class AICustomScript : MonoBehaviour
{
//first waypoint
public Transform Waypoint;
//second waypoint
public Transform Waypoint2;
void Start()
{
rb = GetComponent<Rigidbody>();
//make plane point in direction of first waypoint
transform.LookAt(Waypoint);
}
//allow me to select amount of force in editor
public float AddForceAmount;
//allow me to select plane in editor
public Rigidbody rb;
/void FixedUpdate()
{
//make plane move
rb.AddForce(transform.forward * AddForceAmount);
}
//PART OF CODE TO DETECT ARRIVAL ON WAYPOINT
void OnTriggerEnter(Collider other)
{
if (other.gameObject)
{
//destroys waypoint
Destroy(other.gameObject);
//makes plane look at 2nd waypoint (Waypoint2)
transform.LookAt(Waypoint2);
}
}
}
//SO WHAT'S WRONG?
我在评论中包含了我的逻辑思维。然而,当飞机到达航点 1 时,它转向航点 2,但只是部分转向。所以当它飞向第二个航路点时,它永远不会 "hits" 它。为什么?
截图:
您可能希望使用球面插值法使飞机在改变位置时面向航路点。而不是增加前向力。
using UnityEngine;
using System.Collections;
public class AICustomScript : MonoBehaviour
{
public Transform[] Waypoints;
public float minimumTouchDistance = 2.0f; // minimum distance between the airplane and the waypoint
public float airplaneSpeed = 10.0f;
private Transform currentWaypoint; // keep track of current waypoint
private int currentIndex; // current position in the waypoint array
void Start()
{
currentWaypoint = Waypoints[0]; // set initial waypoint
currentIndex = 0; // set initial index
}
void Update()
{
faceAndMove();
if (Vector3.Distance(currentWaypoint.transform.position, transform.position) < minimumTouchDistance)
{
currentIndex++;
if (currentIndex > Waypoints.Length - 1)
{
currentIndex = 0;
}
currentWaypoint = Waypoints[currentIndex];
}
}
void faceAndMove()
{
Vector3 deltaPosition = currentWaypoint.transform.position - transform.position;
Vector3 calcVector = deltaPosition.normalized * airplaneSpeed * Time.deltaTime;
this.transform.position += calcVector;
this.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(deltaPosition), 4 * Time.deltaTime);
}
}
所以我花了 2 天时间查看这个脚本,但我仍然看不出我做错了什么。我想制作一架简单的 AI 飞机,它可以从一个点飞到另一个点。到达航点 1 后,它应该飞向航点 2。我将在下面包含我的脚本:
using UnityEngine;
using System.Collections
public class AICustomScript : MonoBehaviour
{
//first waypoint
public Transform Waypoint;
//second waypoint
public Transform Waypoint2;
void Start()
{
rb = GetComponent<Rigidbody>();
//make plane point in direction of first waypoint
transform.LookAt(Waypoint);
}
//allow me to select amount of force in editor
public float AddForceAmount;
//allow me to select plane in editor
public Rigidbody rb;
/void FixedUpdate()
{
//make plane move
rb.AddForce(transform.forward * AddForceAmount);
}
//PART OF CODE TO DETECT ARRIVAL ON WAYPOINT
void OnTriggerEnter(Collider other)
{
if (other.gameObject)
{
//destroys waypoint
Destroy(other.gameObject);
//makes plane look at 2nd waypoint (Waypoint2)
transform.LookAt(Waypoint2);
}
}
}
//SO WHAT'S WRONG?
我在评论中包含了我的逻辑思维。然而,当飞机到达航点 1 时,它转向航点 2,但只是部分转向。所以当它飞向第二个航路点时,它永远不会 "hits" 它。为什么?
截图:
您可能希望使用球面插值法使飞机在改变位置时面向航路点。而不是增加前向力。
using UnityEngine;
using System.Collections;
public class AICustomScript : MonoBehaviour
{
public Transform[] Waypoints;
public float minimumTouchDistance = 2.0f; // minimum distance between the airplane and the waypoint
public float airplaneSpeed = 10.0f;
private Transform currentWaypoint; // keep track of current waypoint
private int currentIndex; // current position in the waypoint array
void Start()
{
currentWaypoint = Waypoints[0]; // set initial waypoint
currentIndex = 0; // set initial index
}
void Update()
{
faceAndMove();
if (Vector3.Distance(currentWaypoint.transform.position, transform.position) < minimumTouchDistance)
{
currentIndex++;
if (currentIndex > Waypoints.Length - 1)
{
currentIndex = 0;
}
currentWaypoint = Waypoints[currentIndex];
}
}
void faceAndMove()
{
Vector3 deltaPosition = currentWaypoint.transform.position - transform.position;
Vector3 calcVector = deltaPosition.normalized * airplaneSpeed * Time.deltaTime;
this.transform.position += calcVector;
this.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(deltaPosition), 4 * Time.deltaTime);
}
}