- Home /
How to fix rotation while moving character
Hi, I made a challenge for myself. Within 2 weeks I should create a small game. Here is my first problem. I can't fix the rotation while I'm moving my character. The character acts like crazy. Pls help. Here is my code. Sorry for Eng.
public float horizontalSpeed = 1;
public float verticalSpeed = 1;
public float rotatingSpeed = 13;
private void Update()
{
if (Input.GetKey(KeyCode.Mouse0))
{
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Translate(h, 0, v);
float hs = rotatingSpeed * Input.GetAxis("Mouse X");
transform.Rotate(0, hs, 0);
}
}
}
Comment
Given your tight schedule, maybe the fastest way is to outsource the movement scripting to out of the box CharacterController: https://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html Of course it takes a while to check out how it works, but might still be faster..