Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DroidifyDevs · Jan 14, 2016 at 01:14 AM · c#physicsrigidbodyaddforcelookat

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?
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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. alt text 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.


forces.png (10.8 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robert_s_a · Mar 10, 2017 at 05:28 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ball Speed is not increasing as per code 0 Answers

How to make Airplane move forward faster INSTANTANEOUSLY? 1 Answer

AddForce and Momentum‏ 1 Answer

Kinematic sphere doesn't move 1 Answer

Stuttering with moving Rigidbody 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges