- Home /
Mouse Character Rotation problem :(
On pressing "D" on they keyboard, the character should rotate 90 degrees about its own axis and the code in the LateUpdate is for making the character rotate with the mouse.
Its all working fine but the problem is that whenever i press D and then move the mouse to an arbitrary position, the character snaps back to its last position before mouse was used to rotate the character
This is the code im using-
var newRotation : Quaternion;
function Update(){
if(Input.GetKeyDown(KeyCode.D)){
newRotation = transform.rotation * Quaternion.Euler(0,90,0);
}
transform.rotation = Quaternion.Slerp(transform.rotation,newRotation,Time.deltaTime);
}
function LateUpdate(){
transform.localEulerAngles.y+= Input.GetAxis("Mouse X") * 5;
}
I would like to fix this, like the character shouldn't snap back :S
Answer by lukas_balaz · Jul 09, 2014 at 09:38 AM
In Update() function it is trying to get to newRotation. If the object is rotated with mouse, it is still trying to get to newRotation. Try this:
var newRotation : Quaternion;
function Update(){
if(Input.GetKeyDown(KeyCode.D)){
newRotation = transform.rotation * Quaternion.Euler(0,90,0);
}
transform.rotation = Quaternion.Slerp(transform.rotation,newRotation,Time.deltaTime);
}
function LateUpdate(){
transform.localEulerAngles.y+= Input.GetAxis("Mouse X") * 5;
newRotation.eulerAngles.y += Input.GetAxis("Mouse X") * 5;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613153544im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Cant align players up vector to surface normal 0 Answers
Slowly Change One Object's Rotation To Match Another's 2 Answers
Rotation speed,Rotate script slows down 1 Answer
How do change UI.button onclick states(off, editor and runtime, runtime only) via script? 0 Answers