- Home /
making and object rotate with mouse
hello all iv been looking around for some help on how to make a script on making something rotate with the mouse what i mean is like thing thing the game something like that cause im in the making of a 3d platform game and i want the arms to look at the mouse for the aiming right now im using a script that uses the mouse input to rotate the arms but i want the arms to look at the mouse
example http://img194.imageshack.us/i/runaimfire.png/ http://img713.imageshack.us/i/runaimfire01.png/
the reason it doesn't aim right is cause im using only the y axis so what would i need to do to fix this please help i have no idea how to do this
this is the script im using right now
var verticalSpeed : float = 2.0;
function Update () {
var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate ( -v, 0, 0);
}
if you know how this is done please help :)
ps:this game is called run aim fire look it up on you tube if you like it
Answer by RdJ · Apr 09, 2011 at 07:56 AM
I made a script that should do the trick:
var cam:Camera;
function Update() { var x:float = Input.mousePosition.x - cam.WorldToScreenPoint(transform.position).x; var y:float = Input.mousePosition.y - cam.WorldToScreenPoint(transform.position).y;
var zRotation:float = Mathf.Rad2Deg * Mathf.Atan2(y, x);
transform.eulerAngles = Vector3(0, 0, zRotation);
}
Variable cam is the main camera you're using. Rest of it is just simple maths.
Assign the script to the arm of your character.
wow that really help the only problem now is that the arm aim in a diffident direction but i can fix it
Answer by jeffmadriaga · Mar 03, 2013 at 03:39 AM
Once again like the other posts this works great for a 2d game, thanks!
Is there anyway that the angle returned could be between 0 to 360 instead of -180 to 180?
Answer by Nick 28 · Apr 24, 2011 at 08:51 AM
RDJ, thank you for posting this script, after searching for many hours and trying countless other scripts claiming to work which didn't, this did the job. You restored my faith in the community!
Wow, thanks! I needed this too! LookAt wasn't working for me. I HATE $$anonymous$$ATHS! lol. Cheers!!!!
Your answer
Follow this Question
Related Questions
Multiple rotate with mouse 2 Answers
Rotate Player with Camera rotation 2 Answers
Another way to follow an object? 2 Answers
How do I make one GameObject Point Towards another GameObject in 2D? 1 Answer
GUI Button Question 1 Answer