Answer the question
In order to leave comments, you need to log in
Character rotation towards Unity movement?
The question is that I wrote the character rotation code, it works, but it works crookedly. The character must turn in place, and he turns in the distance. Code below
public class Player : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
private Vector2 moveInput;
private Vector2 moveVelocity;
public Joystick joystick;
private Animator anim;
private bool facingRight = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
moveInput = new Vector2(joystick.Horizontal,joystick.Vertical);
moveVelocity = moveInput.normalized * speed;
if (moveInput.x == 0)
{
anim.SetBool("isRunning",false);
}
else
{
anim.SetBool("isRunning", true);
}
if(!facingRight && moveInput.x > 0)
{
Flip();
}
else if (facingRight && moveInput.x < 0)
{
Flip() ;
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
}
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Answer the question
In order to leave comments, you need to log in
And why not turn left / right to put it where the reading of the inputs takes place directly? Just when the left button is pressed transform.localscale.x =-1; and vice versa to the right
Perhaps the center of your object (pivot) is somewhere else.
Select pivot at the top.
With the blue line I have shown where the Pivot is located. It should be in the center (horizontally) of the picture.
The rotation is relative to this pivot.
You can also rotate not through localScale, but through rotation about the Y axis. So, it will even be more logical
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question