Question by
O-R-C-A · Dec 30, 2020 at 03:40 AM ·
lookrotation
LookRotation causes object keep walking in circle
Hello,
I am a beginner and trying to make First person game.
When I use "Quaternion.LookRotation", when the left, right or down button is pressed, the character keep walking in circle, instead of turn to the direction of the button.
Thank you for advice!
public CharacterController characterController;
private Vector3 prev;
void Start()
{
anim = gameObject.GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody>();
Player_pos = GetComponent<Transform>().position;
m_AudioSource = GetComponent<AudioSource>();
prev = GetComponent<Transform>().position;
}
void Update()
{
bool isWalking;
if (Input.GetAxis("Horizontal") != 0 || Input.GetKey("Vertical"))
{
isWalking = true;
}
else
{
isWalking = false;
}
anim.SetBool("IsWalking", isWalking);
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
rb.velocity = transform.right * horizontal + transform.forward * vertical;
if (characterController.isGrounded)
{
if (Input.GetAxis("Jump") > 0)
{
rb.velocity += new Vector3(0, 10 / jumpSpeed, 0);
}
}
rb.velocity += new Vector3(0, Physics.gravity.y * Time.deltaTime, 0);
characterController.Move(rb.velocity * walkSpeed);
Vector3 diff = transform.position - prev;
diff.y = 0;
if (diff.magnitude > 0.01)
{
transform.rotation = Quaternion.LookRotation(diff);
}
prev = transform.position;
if (isWalking)
{
if (!m_AudioSource.isPlaying)
{
m_AudioSource.Play();
}
}
else
{
m_AudioSource.Stop();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Delayed LookAt, but only on one axis 1 Answer
how to make 2d sprite flip vertically while looking at the mouse 0 Answers
Turret rotating on a z axis 2 Answers
How do I combine this smaller code with my larger code? (I am a beginner please help :CC) 0 Answers
How to face towards vertical collider? 0 Answers