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 Taxen0 · Jun 01, 2014 at 10:22 PM · gameobjectangle

Get the angle of a gameObject or correct rotation

Hello,

I need to get the angle of a gameObject so i can use it to shoot a projectile in the same direction.

 Vector2 force = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)) * bulletSpeed;
     
 projectile.rigidbody2D.velocity = force;

This is the snippet I plan to use to shoot the newly created projectile and when i enter a angle, like 90, 45, 270 or some other value it seems to fire correctly.

Now I "just" have to get the angle automatically from an object named spawn that is the child of a object that rotates (so I guess local coordinates/angels won't work).

How would I go about doing this? I have been at it for hours now..

Thanks in advance!

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 Jeff-Kesselman · Jun 01, 2014 at 11:02 PM

transform.forward is a normalized vector pointing down the local Z axis.

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 Taxen0 · Jun 02, 2014 at 12:38 PM 0
Share

Cheers, that seems a lot simpler!

I did have to use transform.right since I don't have the blue axis but it seems to do what I want.

However when my character faces left it still shoots to the right, it is the correct angle but in the wrong direction.

I guess this has something to do with the movement script that I use (the sample assets from unity). this is what it does then you turn:

 void Flip ()
         {
             // Switch the way the player is labelled as facing.
             facingRight = !facingRight;
             
             // $$anonymous$$ultiply the player's x local scale by -1.
             Vector3 theScale = transform.localScale;
             theScale.x *= -1;
             transform.localScale = theScale;
         }

Is there a way to counter this effect for this calculation? Or is there anything else that is messing around?

the rotating object (the arm) and the empty spawn object are children of the character that this is used on.

avatar image Taxen0 · Jun 02, 2014 at 08:19 PM 0
Share

Here are some images to explain this more clearly: FACING LEFTFACING RIGHT

So, is there a way to get the red axis stay in the correct angle? even when its parent is flipped around.

Otherwise I have to make 2 separate animations for every action so I don't flip around, but I was hoping to save me that hassle. cheers!

avatar image Taxen0 · Jun 06, 2014 at 10:27 PM 0
Share

Seems to be a bug/"feature" in unity, simply don't flip the scale when dealing with children. make separate sprites for left/right =(

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

23 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

Related Questions

Trouble with getting an instantiated object to travel at an angle using force 1 Answer

Set a steady angle between gameObject and Screen 0 Answers

How to know 3DGameObject rotating clockwise or counterclockwise 1 Answer

Draw only part of a circle 0 Answers

Mouse drag with specific angle 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