- Home /
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!
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
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.
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)
Your answer
![](https://koobas.hobune.stream/wayback/20220611234719im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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