- Home /
Pointing object to Mouse X and Z
I'm trying to make a shooter game similar to Angry Bots. But I'm having trouble rotating the object in relation to the mouse. (top camera view and rotation Z and X)
I tried this code but... When the mouse is in the top half of the screen it rotates correctly When the mouse is in the bottom half of the screen it rotates the opposite way
var speed : float = 1.0;
function Update(){
transform.Rotate(Vector3(0 , Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Z")) * Time.deltaTime * speed);
}
Answer by pnoct · Jan 10, 2012 at 05:54 AM
Do you mean rotating an object to face the mouse pointer? If so, you will need to use trigonometry to do it. I think that using tranform.rotate takes an argument of 3 angles to rotate an item around the three axes, so you might need to calculate the angle based on the position of the object relative to the mouse. If this is what you want to do, look up the sine and cosine formulae to do it. I think Transform.LookAt might do this for you, but not sure.
Your answer
Follow this Question
Related Questions
How do I make gameObject.transform.rotation.z equal to a set float value? 2 Answers
Rotating objects around player using lerp 0 Answers
Rotate Towards doesnt work as expected 2 Answers
How to make top-down objects rotate to face another? 1 Answer
How to make object lean in the direction it's going 3 Answers