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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Fushiru · Jun 21, 2015 at 11:16 AM · rotationweaponfacing

find the angle to rotate the gun to face mouse point with its barrel

Hi, it's my first question here. I couldn't find the answer anywhere, so after searching through Internet and not finding anything I finally gave up. Maybe someone will help. I have a problem with my weapon rotation script trying to face mouse pointer.
Basically, what I have is weapon object with fire point object as a child of this weapon object. Fire point is placed on gun barrel sprite. I want to rotate the weapon to make its fire point face mouse point, as opposite to weapon's origin facing mouse (there are so many guides to do this..).
My best approach so far is using regular rotating script based on Atan2, and it works only to certain degree. And by that I mean, that when I have my mouse pointer far from weapon, then everythings seems ok, but then when I hover near, the weapon starts acting weird, shaking and what not. Here's the code:

     Vector2 mousePosition = camera.ScreenToWorldPoint(Input.mousePosition);

     Vector2 distance = mousePosition - (Vector2)weaponTransform.position;

     float angle = Mathf.Atan2(distance.y, distance.x) * Mathf.Rad2Deg;

     //if facing left
     if(characterTransform.localScale.x < 0){
         angle = -angle + 180;
     }

     weaponTransform.rotation = Quaternion.Euler(0, 0, angle);

     Debug.DrawRay(firePointTransform.position, distance, Color.red);




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 Socapex · Jun 21, 2015 at 11:26 AM

transform.LookAt, Vector3.RotateToward, Quaternion.RotateTowards all seem like good options. Remember you can change your rotation anchor to fit the object. Also, getting "weird things" is inevitable when the mouse will get close. You can simply disable rotations in a close radius around your gun. Or at least do something elegant when mouse is close.

Comment
Add comment · 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

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 can I rotate a rigid body based on its velocity? 3 Answers

orienting player in one direction 1 Answer

How do I make sure a object faces a certain direction? 1 Answer

how can i substract one axis by 180? 2 Answers

How do i rotate something on a new rotation axis? 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