- Home /
Question by
Snehil_Jha · Jun 09, 2020 at 08:54 PM ·
c#movementmousebug-perhaps
WSAD controls work fine for W and S, but randomly get reversed for A and D
I was creating a 3d game with player movement and camera following the mouse. For player movement this is what I did :- //Part of Update
float sinRotationAngle = Mathf.Sin(camera.transform.rotation.x);
float cosRotationAngle = Mathf.Cos(camera.transform.rotation.x);
//Part of FixedUpdate
if(moveForward){ // I check for movement in Update
player.rigidbody.AddForce(moveSpeed * sinRotationAngle * Time.deltaTime, 0,moveSpeed * cosRotationAngle * Time.deltaTime, ForceMode.VelocityChange);
}
if(moveBackward){
player.rigidbody.AddForce(-moveSpeed * sinRotationAngle * Time.deltaTime, 0,-moveSpeed * cosRotationAngle * Time.deltaTime, ForceMode.VelocityChange);
}
if(moveLeft){
player.rigidbody.AddForce(moveSpeed * cosRotationAngle * Time.deltaTime, 0,moveSpeed * sinRotationAngle * Time.deltaTime, ForceMode.VelocityChange);
}
if(moveRight){
player.rigidbody.AddForce(-moveSpeed * cosRotationAngle * Time.deltaTime, 0,-moveSpeed * sinRotationAngle * Time.deltaTime, ForceMode.VelocityChange);
}
Sometimes it works perfectly fine, but randomly when it is supposed to move right, it moves left and vice-versa. If anyone can tell me what is wrong in my code, or if this is a bug in unity, it would be very helpful. And I am new to Unity Answers, so I don't know how to format my stuff.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613000409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
How do I make my character lean to the sides? 0 Answers
Call an exorcist. 1 Answer