x


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

more ▼

asked Apr 09 '11 at 05:56 AM

josifjoey gravatar image

josifjoey
5 6 8 11

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Apr 09 '11 at 07:56 AM

RdJ gravatar image

RdJ
47 4 4 7

wow that really help the only problem now is that the arm aim in a diffident direction but i can fix it

Apr 09 '11 at 01:08 PM josifjoey
(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Apr 24 '11 at 08:51 AM

Nick 28 gravatar image

Nick 28
1

Wow, thanks! I needed this too! LookAt wasn't working for me. I HATE MATHS! lol. Cheers!!!!

Sep 11 '12 at 03:48 AM POLYGAMe
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Mar 03 at 03:39 AM

jeffmadriaga gravatar image

jeffmadriaga
0 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1002
x747
x338

asked: Apr 09 '11 at 05:56 AM

Seen: 2663 times

Last Updated: Mar 03 at 03:39 AM