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 /
avatar image
0
Question by FireWasTaken · Jul 13, 2016 at 05:06 PM · rotationmathangles

Problem with angles and rotations in a 2D top down space shooter

Hello everyone. I'd like to start by saying that im very new to both unity and c#, so my question might be really really dumb. Im also pretty awful at math so that doesnt help with the "angle problem" I got.

Basically, Im trying to make a enemy turret slowly rotate towards the player. Since I want the rotation to be slow, Im using transform.Rotate instead of transform.rotation, as I dont know a way to make the second one be slow and not instant.

My logic is: I calculate the angle between the player ship and the enemy turret, and then I transform.Rotate the turret accordingly till the 2 angles are the same , so the turret is aiming at the player. Here's how I get the angle between the player ship and the player :

 float AngleBetweenPoints(Vector2 a, Vector2 b)
 {
     return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
 }

this returns a float that goes from 180 to -180. now, the issue starts here. in my script to rotate the turret, I check the "transform.eulerAngles.z ", that would work but the turret angle goes from 0 to 360, while the AngleBetweenPoints goes from 180 to -180 as I said. so when I use this script (attached to the turret) it works aslong and the AngleBetweenPoints is POSITIVE, but it goes bananas when the AngleBetweenPoints is negative...

void RotateTurret() {

     if (transform.eulerAngles.z < (angle ))
     {
         transform.Rotate(0, 0, 75 * (Time.deltaTime ));
     }
     if (transform.eulerAngles.z > (angle))
     {
         transform.Rotate(0, 0, -75 * (Time.deltaTime ));
     }
 }

as you can see i check the transform.eulerAngles.z of the turret and i compare it with the angle ( that is the angle between the ship and the turret) and then I use transform.Rotate and multiply by time.deltaTime to slow down the rotation.

As I said, it works till the angle is positive, but when it goes from 0 to -180 it fucks up the script. Ideally, I'd want to either convert the angle between points to a float that goes from 0 to 360, or convert the turret angle to a float that goes from 180 to -180. as I already said Im pretty crap at math, and even if I worked on this most of the night and morning I cannot find a way out. if you have any solution regarding the angle problem, or know any simpler way to accommplish what Im trying to do, please help me, Im miserable.

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

0 Replies

· Add your reply
  • Sort: 

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

Need help to get 3D rotation from 2 points in space 1 Answer

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers

Issue finding an angle with trigonometry 1 Answer

Rotate an Object by a spezific value 2 Answers

How to check object "rotated" X degrees 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