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 Catlard · Apr 20, 2012 at 05:29 AM · directionangleforwardmissile

Heat-seeking missile code.

Hey! So, I'm implementing heat-seeking missiles in a platformer game. Basically, I'm attempting to code a kind of inertial movement where the missile goes after an object (in this case, a helicopter) in the most efficient way possible. But that route will be limited by its turning radius--it won't be able to turn fast enough if the helicopter "dodges" it by moving out of the way.

In any case, my strategy is to constantly adjust the angle towards the helicopter, and then just push the copter in the direction of a vector I'm constantly changing based on that rotation. I'm having trouble with the second part of that process. It seems to be doing something right, at times--because the missile generally will go in the direction of the helicopter. Any idea what I'm doing wrong, here? Let's assume, for the purposes of sanity, that the missile always starts at angle 0.

   void Update () 
     {
         AdjustAngle();
         transform.position += directionVector; // updated in adjustangle
     }
     
     public void AdjustAngle()
     {
         //aim for the target. move an increment toward the target if it's not aiming at it within a certain range.
         //move in the direction the missile is facing.
         angleOfChopper = GetAtan2Deg();
         maxMoveDistance = missileSpeed * Time.deltaTime;
         amountToRotate = missileTurnRadius * Time.deltaTime;
         if(angleOfChopper < transform.eulerAngles.z) 
         {
             transform.Rotate(0,0, -amountToRotate);
             directionVector = new Vector3( -1* maxMoveDistance * Mathf.Cos(amountToRotate), maxMoveDistance * Mathf.Sin(amountToRotate) , 0);
         }
         if(angleOfChopper >= transform.eulerAngles.z) 
         {
             transform.Rotate(0,0, amountToRotate);
             directionVector = new Vector3( maxMoveDistance * Mathf.Cos(amountToRotate), -3 * maxMoveDistance * Mathf.Sin(amountToRotate) , 0);
 
         }
         
         
     }
     
     public float GetAtan2Deg()
     {
         float yDist = gameObject.transform.position.y - chopper.transform.position.y;
         float xDist = gameObject.transform.position.x - chopper.transform.position.x;
         return Mathf.Rad2Deg * Mathf.Atan2(yDist, xDist);
     }
 }
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
1
Best Answer

Answer by senad · Apr 20, 2012 at 06:42 AM

The logic in MoveForward() seems too complex to me.

Would it not be much easier to keep a direction vector which you rotate with the missile? When moving forward, you just add it to the missile position. That would be only one line of code for the missile movement.

Comment
Add comment · Show 6 · 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 Catlard · Apr 20, 2012 at 06:53 AM 0
Share

Ah, good plan, let me try that...

avatar image Catlard · Apr 20, 2012 at 07:11 AM 0
Share

Hmm...I know what you mean, but how do I deter$$anonymous$$e what the vector will be adjusted by?

avatar image Catlard · Apr 20, 2012 at 07:35 AM 0
Share

Ok, so I've updated my code based on what I think you're saying I should do...but it still appears to be brokentown. Any idea what I'm doing wrong?

avatar image senad · Apr 20, 2012 at 08:13 AM 0
Share

Is it the same wrong behaviour as before?

$$anonymous$$ake sure that your direction vector points in the same direction as the missile.

Also, if you have any physics enabled, it will not work this way, because the physics will conflict with your manual transformations.

If it still does not work, you can post your current code.

avatar image Catlard · Apr 20, 2012 at 12:17 PM 0
Share

Hey, Senad, thanks for responding! No, it's not the same incorrect behaviour. Now it seems to make the same motion every time, regardless of how I modify the code. It sort of rotates around the point it's supposed to head towards... In any case, I've reposted the newer code over the older code in the original post...what do you think, now?

Also, no rigidbody on the missile, so no worries about that.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add slight angle/rotation variation to transform.forward? 5 Answers

Making player face the direction it moves (using angles). 1 Answer

Converting direction to Vector2 2 Answers

Instantiate at a right angle from collision impact 1 Answer

Finding the direction from an angle in 3d space 2 Answers


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