The problem is when i look up the character starts hopping. Pointing out any other issues i might encounter with this setup would be great as well, since i'm sure this could be done better.
CharacterController c = GetComponent();
if (x < 0) { x += 4*Time.deltaTime; } if (z < 0) { z += 4*Time.deltaTime; }
if (x > 0) { x -= 4*Time.deltaTime; } if (z > 0) { z -= 4*Time.deltaTime; }
if (x < -2) { x = -2; } if (z < -2) { z = -2; }
if (x > 2) { x = 2; } if (z > 2) { z = 2; }
lr += 2*Input.GetAxis("Mouse X"); ud -= 2*Input.GetAxis("Mouse Y");
if (ud > 30) { ud = 30; } if (ud < -30) { ud = -30; }
transform.eulerAngles = new Vector3(ud,lr,0);
if (c.isGrounded)
{
if (Input.GetKey(KeyCode.D)) { x += 1; } if (Input.GetKey(KeyCode.A)) { x -= 1; }
if (Input.GetKey(KeyCode.S)) { z -= 1; } if (Input.GetKey(KeyCode.W)) { z += 1; }
md = transform.TransformDirection(new Vector3(x,0,z));
if (Input.GetKeyDown(KeyCode.Space)) { md.y = 2; }
}
md.y -= 4*Time.deltaTime;
c.Move(md*Time.deltaTime);
↧