Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Master2112 · Nov 03, 2011 at 07:12 PM · rotationaifollow playerairplanelookattarget

Follower Airplane problem

Hi,

i made a script for controlling an airplane (and the entire airplane's behaviour) two days ago, just to see if i could. Then, i figured it would be fun to have another tiny drone following me around.

I copied the entire script from my airplane, and instead of controlling it with keys, i wanted it to check how it should move by comparing it's rotation and position to mine.

Everything went fine, untill i did the y rotation. If it's rotation was 10, and the direction to my plane was 350, it would turn 340 degrees to the right instead of the (for humans) logical 20 degrees to the left.

I tried a few things to fix that, but was unsuccessful. When looking for other solutions, i found this website, and decided to ask for help here.

Here is the entire code for my drone follower! (C#!) (sorry for for the uncommented mess beneath, never thought i would have to show it to others. If you want to help, but don't know what some of the variables are supposed to do, feel free to ask :P (some of the declared variables aren't even used, for example.))

Also, my e-mail is timfalken@hotmail.com!

125*------- Le Code ------*125

 using UnityEngine;

using System.Collections;

public class FollowerDrone : MonoBehaviour {

public float snelheid;

float stuur = 0f;

float stijging = 0f;

public float gravity = 0f;

public float throttle = 1f;

float floatheight =0.01f;

bool floatup = true;

 //AI STUFF//

 

 public bool Fly = true;

 int steer = 0;

 public GameObject target;

 float distancetotarget;

 Vector3 angletotarget;

 

 //END AI STUFF//

 

 float zrotate = 0f;

 // Use this for initialization

 void Start () 

 {

 

 }

 

 // Update is called once per frame

 void Update () {

 //Vector3 pos = transform.position;

     transform.position += transform.forward * (snelheid*throttle);

     Vector3 pos = transform.position;

     if (Fly == true)

     {

     if(throttle<1)

     {

     throttle+=0.005f;

     }

     }

     else

     {

         if(stijging>0)

         {

             

             if(throttle>0)

             {

                 throttle-=0.005f;

                 

             }

             

         }

         else

         {

             if(pos.y>0)

             {

             if(throttle<1)

             {

             throttle+=0.005f;

             }

             }

             else

         {

             if(throttle>0)

             {

             throttle-=0.005f;

             }

         }

         }

             

         

         

     }

     

     if(pos.y>0)

                 {

             if(stijging<90 && stijging>-90)

             {

                     stijging-=(0.4f*(1-throttle));

             }

         else

             {

                     stijging+=(0.4f*(1-throttle));

             }

         }

     

     pos.y -=  gravity*(1-throttle);    

     

     

     

     if(pos.y<0)

     {

         pos.y+=(-pos.y)*0.1f;

     }

     

     transform.position = pos;        

     

     

     

     

     //AI UPDATE

     

     var targetPoint = target.transform.position;

       var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);

     print(targetRotation);

         //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0); 

     float horizontaal =targetRotation.y + transform.rotation.y;

     float verticaal  = 0;

     

     

     if(target.transform.position.y > transform.position.y + 10 && stijging <50)

     {

         verticaal = 1;

     }

     else

     {

     if(target.transform.position.y < transform.position.y - 10 && stijging >-45)

     {

         verticaal = -1;

     }

         else{verticaal=0;}

     }

     //float horizontaal = transform.rotation.y + targetRotation.y;

     //END AI UPDATE



     stuur += horizontaal;

     

     stijging += verticaal;

     stijging = stijging*0.99f;

     zrotate -= horizontaal;

     zrotate = zrotate*0.99f;

     if(stijging>180)

     {

     stijging=-180;    

     }

     if(stijging<-180)

     {

     stijging=180;    

     }

     

     if(zrotate>90){zrotate=90;}

     if(zrotate<-90){zrotate=-90;}

     

     transform.rotation = Quaternion.Euler(-stijging, stuur, zrotate);



 }

}

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
Best Answer

Answer by MattWhiting · Nov 04, 2011 at 07:32 PM

I found this helper function in Unity's math library: [MoveTowardsAngle][1] http://unity3d.com/support/documentation/ScriptReference/Mathf.MoveTowardsAngle.html

Here's a thread on wrapping angles: [wrapping-euler-angles-to-achieve-mouse-look][2]

To save you some time, the easy-to-understand and correct answer is this:

 float WrapAngle(float ang) {
     while (ang > 180.0f)
         ang -= 360.0f;
     while (ang <= -180.0f)
         ang += 360.0f;
     return ang;
 }

the more mathematically efficient version is this:

 float WrapAngle(float ang) {
     int n = (int)(ang / 180.0f);
     if (n & 1)
         n += (n < 0 ? -1 : 1);
     return ang - (float)(n * 180);   
 }
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 Master2112 · Nov 06, 2011 at 09:12 AM 0
Share

(just found out i can comment, i'm new :P) Thanks for the code! ill try it and tell you if i got it to work :)

Le Edit: trying to get my plane to use it, but it would help if i knew how to put the code in my plane.

Le Second Edit: Got it to work! thanks a lot :P

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you make enemies rotate to your position in 2d 2 Answers

AI Airplane Problem 0 Answers

Simulating Mouse Input for AI 0 Answers

How do I have multiple AI that follow the player but don't move into each other? 1 Answer

How to make a rigidbody rotate towards an object using physics 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