Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Evaquate · Aug 06, 2015 at 06:07 PM · scripting problemuigameobject

Rotate GameObject When Mouse/Player is Within Certain Distance From it

Hello, I am trying to make a simple 2D app where a ball is fired from a cannon and directed to the target. The cannon is meant to rotate in the direction of the player’s finger, and up until now, I was using mousePosition, and that worked fine. However, I’d like to have this enabled only when the finger/mouse cursor is within a certain distance from the cannon. I was thinking of having a button attached to the cannon GM, and have the cannon do its rotating when the player is hovering over the button (I was thinking the button UI would also let me do a highlighting effect when the player is close enough to rotate the cannon). I’m still learning the ropes when it comes to programming, and don’t even know if this is the best way to do this.

This is what I have to rotate the cannon, I’d like for it to check if the player is in the button’s area before executing the code:

 Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
 difference.Normalize ();
 float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotOffset);

Any help is appreciated, and 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
1
Best Answer

Answer by troien · Aug 06, 2015 at 06:35 PM

If you already have a working script that rotates your object towards your cursor, you could simply use use Vector3.Distance (or Vector2.Distance). Doing this via UI is also possible, but probably harder as scaling this UI item correctly to your object might be difficult on the various resolutions posible...

Something like this:

 if (Vector3.Distance(Vector3.zero, difference) < 2)
     // You should get here if you are less then 2 units away...


Another thing which might be better in your case sinse you already calculated the difference is Vector3.magnitude

 if (difference.magnitude < 2)
         // You should get here if you are less then 2 units away...


ps. This check should abviously happen before normalizing the difference though :p

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 Evaquate · Aug 07, 2015 at 04:03 PM 0
Share

Thanks a bunch! difference.magnitude works great, and I have a working (but quite makeshift) way of incorporating the button UI. However, the direction where the cannon fires the ball doesn't reflect the direction of the cannon itself (the ball still fires in the direction of the mouse cursor, even when the cannon isn't following the cursor anymore). How would I get the current direction of the cannon/where it's actually pointing as opposed to the direction of the mouse?

The following code is what I have right now, and while it lets me rotate the cannon the way I want it, I can't make the direction the ball is fired "difference", as it would still get the direction of the mouse ins$$anonymous$$d of the cannon's direction.

         Vector2 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
         if (difference.magnitude < 150){
             difference.Normalize ();            
             float rotZ = $$anonymous$$athf.Atan2 (difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
             transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotOffset);
         }

Apologies for the followup question, and if I was supposed to put it as a new question ins$$anonymous$$d of a comment.

avatar image troien · Aug 07, 2015 at 05:46 PM 0
Share

Well... Sinse I don't know the code that you use to fire the bullet (or the code that is use to move the bullet) I can't really help you with that :p

Gonna try though :p

Depending on the situation I would say you want to move the bullet in the diretion of the 'forward' of the gun. (using something like bullet.transform.rotation = gun.transform.rotation; on instantiating the bullet) and then after instantiating move the bullet over it's own forward (or up, depending on what 'forward' actually is in your 2d game).

If you have no idea what I'm trying to say here then I would advise you to create a new question and include the code that you use for firing the bullet. This because follow up questions can be very messy and don't always get the best answer (as questions that have been answered are not visited as much as unanswered questions)

avatar image Evaquate · Aug 07, 2015 at 06:33 PM 0
Share

Works perfectly, thanks so much!

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

Ingame music goes on but it should stop when you die (DeathCanvas Activate) or win (LevelComplete Canvas Activates) 1 Answer

Why is my Unity script overwriting other game objects on which it is attached to? 1 Answer

UI Button doesn't change interactable to false 0 Answers

How to access the properties of any created Game Object from another script Unity C # 2D 1 Answer

Camera switch between child back to main camera issue 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