Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Fiberwire · Feb 17, 2016 at 12:45 PM · rotationprojectileangleshoming

How to figure out which direction to turn a projectile to face an enemy

I'm making a homing system for the projectiles in my 2D game. The projectiles move in whichever way they are facing. I basically just want to rotate the projectile while it's moving until it's facing the enemy that it's homing onto, which, given that it moves in the direction that it's facing, means it will eventually collide with the enemy. The problem is, I don't know how to determine which way (clockwise or counterclockwise) would be the shortest rotation.

Given this situation: (Projectile left, enemy right), It's easy to see that counterclockwise would be the shorter rotation. alt text

But I'm not quite sure how to calculate that.

These are examples of how the path of the projectile should look (roughly): alt text

I don't want the projectile to move in a straight line to the enemy, but rather smoothly turn towards it as it's moving.

2.png (42.7 kB)
5.png (45.2 kB)
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 Glurth · Feb 17, 2016 at 03:25 PM

To answer your question specifically: take a look at this function, which always returns the smaller angle, (though I'm not sure if it ill give the angles' direction too). http://docs.unity3d.com/ScriptReference/Vector3.Angle.html

That being said, I suggest you also take a look at http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

This transform function will enable you to point your object at the target, with a single function call. something like (uncomplied example)..

 pointyobject.transform.LookAt(targetObject.transform);

Comment
Add comment · Show 3 · 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 Fiberwire · Feb 17, 2016 at 04:27 PM 0
Share

Would it be possible to get the rotation that LookAt() would result in so that I could lerp it to that over multiple frames ins$$anonymous$$d of in one call? I'm trying to get the projectile to make an arc which would end up with it facing the enemy. I don't just want it to do an about face if it's facing away from the enemy, but rather turn (at an arbitrarily specified rate) towards the enemy.

avatar image Fiberwire · Feb 17, 2016 at 09:51 PM 0
Share

I tried using transform.LookAt(). I'm not sure it was meant to be used with 2D games. It makes the sprite stand straight up in relation to the Z axis, which is not what I'm going for.

avatar image Glurth Fiberwire · Feb 18, 2016 at 03:48 PM 0
Share

Ah, to compute the final orientation, so you can lerp, use the static Quaternion Class version of this function: Quaternion.LookRotation() (http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html) This returns a quaternion (which is what the rotation component of the transform is). You can them use the Quaternion Lerp function (http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html)

Regarding the fact that it is not pointing in the plane that you want: that's what the second parameter in the LookAt functions are for. In you case, you probably want to specify a Vector3 that points upwards into the Z axis (Vector3.forward)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Set Instanatiated Object's Rotation Towards Player 1 Answer

Changing a projectile's angle ? 0 Answers

Orbiting Crosshair issue when trying to stop at a specific angle 0 Answers

Rotation before 0 and after 360 0 Answers

Need solution for rotation with player input of X, Y, Z 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