Script makes plane point in general direction but doesn't point fully... read description please!
So I wrote this simple AI script for an airplane to move between waypoints, and wrote my thinking in the comments. However, after the plane destroys the 1st waypoint, it moves partially into the direction of the 2nd waypoint, but never hits the 2nd waypoint. So while it is moving slightly into the right direction... it never actually hits Waypoint2. What am I doing wrong? THANK YOU SO MUCH!
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?
Answer by Robert_s_a · Mar 10, 2017 at 04:14 AM
Hi DroidifyDevs,
I think your problem is with physics.
I tried this script, and it worked fine as long as my player object started on the line which would connect the two waypoints. If it was not on that line it would not. Basically, what's happening is that your plane turns to look at the first waypoint and you add a force along that vector. Once that waypoint is destroyed, your plane turns to look at the second waypoint, and you add a force along that vector. However, your plane is still carrying the momentum from your original force, so the new force gets added to the existing momentum and your plane, while pointed in the right direction, will actually move in a direction resulting in the sum of the two vectors. Unfortunately, I'm not adept enough at the Unity engine yet to know how to procedurally negate a rigidbody force. This is something I'm trying to figure out too.
If anyone out there can take this one step further, I'm sure DroidifyDevs will appreciate it as much as I will.
As a follow-up, I adjusted the code to have the plane LookAt Waypoint2 with every iteration rather than just at the point where the first waypoint is destroyed, and I achieved an orbit around Waypoint2. It still has that angular momentum affecting the force, even if the new force is being constantly applied directly toward Waypoint2. In order to compensate for this you'd need to deter$$anonymous$$e how much you need to over-steer to make the blue arrow above point at Waypoint2, and probably adjust it for each frame. Not sure of the math for this one for an AI routine. But, it would make the simulation more realistic, since a plane would have to turn in an arc and compensate for its existing momentum. It wouldn't make a sharp turn and fly in a straight line from W1 to W2.
Your answer
Follow this Question
Related Questions
Ball Speed is not increasing as per code 0 Answers
How to make Airplane move forward faster INSTANTANEOUSLY? 1 Answer
How do I get objects in front of the player to be blasted away? 1 Answer
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
How to remove addforce effect 0 Answers