- Home /
Mouse look/rotate
I need help with making script that will rotate character left and right based on Mouse Y axis movement and that will look up and down based on Mouse X axis movement. I've tried several methods including Ray, Quaternion and transform.rotate and nothing works for my problem.
have you tried the $$anonymous$$ouseLook script which comes with unity?
Tried this for mouse rotation
var speed = 5;
function Update () { if(Input.GetAxis("Horizontal")) {transform.Rotate(Vector3.right speed Time.deltaTime);}
}
It should rotate object only on horizontal axis, but ins$$anonymous$$d it rotates obj on both horizontal and vertical axis, but inverted. When I move mouse forward it rotates to right etc.
Have you tried using Vector3.up ins$$anonymous$$d of Vector3.right?
Answer by clunk47 · Dec 15, 2012 at 01:18 AM
Why would you want the mouseX(left and right) to look up and down? and mouseY(up and down) to look left and right??? For one, there's a built in Mouselook script in the CharacterController asset for unity. Horizontal Axis Input is for "a" and "d" and left and right arrows. Vertical Axis is "w" and "s", and up and down arrows. You need GetAxis("Mouse X") or GetAxis ("Mouse Y"). And if you use this in an IF statment you need to ask if it that axis is greater or less than zero.
if(Input.GetAxis("Mouse X") < 0)
transform.Rotate(Vector3.up) * speed;
if(Input.GetAxis("Mouse X") > 0)
transform.Rotate(Vector3.up) * -speed;
That's just an EXAMPLE. There are many ways. I say Vector3.up there because that's the y axis of your player or w/e your rotating, meaning it will rotate left and right.
You could also do it like:
transform.localEulerAngles.y+= Input.GetAxis("Mouse X") * speed;
Of course in C# you have to put localEulerAngles in a temp var but that's not the question so I'll stick w/ it. To get you started I'll just lay out that you need to use the mouse axes I named above. If you need help beyond that let us know.
$$anonymous$$y bad on X and Y axis, I accidentally switched them, guess that happens when you don't sleep enough. Anyway, thanks for your help but I've already solved my problem.
I creates an error because unity says "Operator '*' cannot be used with a left hand side of type 'void' and a right hand side of type 'float'"
@Bichetordue To resolve the error, you must wrap the (Vector3.up) speed within a parenthesis. So, ((Vector3.up) speed) will produce no error.
Answer by stratos_kakalis · Mar 06, 2016 at 08:11 PM
Maybe this is too late but for anyone watching this out there you could simply make a "mouseInput" variable (of course you pick the name) and assign it to Input.GetAxis's the set your transform.Rotate(mouseInput); of course im using C# you might have to make some slight modifications to it
forgot to mention that the reason why i suggest this is that it takes about 3-4 lines of code
Here's my C# code to make this work (no character controller or rigidbody):
void Update () {
float mouseInput = Input.GetAxis("$$anonymous$$ouse X");
Vector3 lookhere = new Vector3(0,mouseInput,0);
transform.Rotate(lookhere);
}
To adjust speed of rotation, adjust Sensitivity in edit > Project Settings > input > $$anonymous$$ouse X
I have been looking for something like this for hours! Thank you so much!!!
Your answer
Follow this Question
Related Questions
Look around when holding down mousebutton 1 Answer
Movement along X and Z axis... 2 Answers
Look at mouse 1 Answer
How can I let my Sprite look towards the mouse in Unity 4.3 2D? 1 Answer
Mouse Look Script Help 2 Answers