- Home /
Question by
zidav111 · Oct 01, 2020 at 07:52 PM ·
animationquaternionsc# tutorial
How to rotate player controller properly
i made a joystick that triggers an animation while controlling the player to move in that direction. so i searched on your youtube and they used quaternion to solve it. it did solve my problem but it started making the screen shake anytime i move. what can i do???
heres the code
void joystickControls()
{
if (joy.Horizontal != 0f || joy.Vertical != 0f)
{
anim.GetComponent<Animator>().Play("WalkForward");
transform.rotation = Quaternion.LookRotation(rb.velocity);
}
if (joy.Horizontal == 0f && joy.Vertical == 0f)
{
anim.GetComponent<Animator>().Play("Idle");
}
}
Comment
Answer by andrew-lukasik · Oct 02, 2020 at 06:42 AM
Try
[SerializeField] Animator _animator = null;
[SerializeField] Rigidbody _rigidbody = null;
void JoystickControls()
{
Vector3 velocity = _rigidbody.velocity;
Vector3 velocityXZ = new Vector3( velocity.x , 0 , velocity.z );//ignores Y (falling, jumping)
float speedXZ = velocityXZ.magnitude;
if( joy.Horizontal!=0f || joy.Vertical!=0f )
{
_animator.Play("WalkForward");
if( speedXZ>0.01f )
{
transform.rotation = Quaternion.LookRotation( velocityXZ , Vector3.up );
}
}
else
{
_animator.Play("Idle");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613012930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Can I make animations snap to a frame? 1 Answer
Animate a button. 0 Answers