- Home /
Rotate object with mouse
probably a better idea on what I want, Ive it to spawn the bullets, but i just need it to rotate on a fixed position like the image is showing. Someone guide me on how to do this please
Answer by robertbu · Dec 01, 2013 at 05:58 PM
There are many different ways depending on game. Here is one way:
#pragma strict
var speed = 0.5;
function Update () {
transform.Rotate(0.0, 0.0, -Input.GetAxis("Horizontal") * speed);
}
To use this script, go to Project Settings > Input and make sure that the entry for 'Horizontal' has the 'Type' set to 'Mouse Movement'. In addition, if you plan on using multiple different screen resolutions, you will likely need to scale 'speed' as appropriate to the resolution.
Ive done it how told me to, but its only rotating the object, I need it so it goes left and right and rotate on the axis in a fixed position. something like this
Your actual question states, "but i just need it to rotate on a fixed position." So now there is an issue about how to interpret the mouse movement. Should the movement push the tank left and right, or should it rotate the tank. Or is there some sort of key or switch that changes between the two?
sorry i just realised that my title isnt the best description to what i want. the only object that should be moving is the turret
http://www.youtube.com/watch?v=H3i0HLCrdSk its not the best example
I still don't completely understand. I change my code above so that it now rotates around the 'z' axis. In the video, the turret does not move left and right, just the gun angles left and right. The code above will do that. If you are trying to mix movement and rotation (i.e. the gave above mixed with Alien Invaders), then you will have to specify how the inputs are done.