K
K
kovurado2021-08-01 22:17:59
C++ / C#
kovurado, 2021-08-01 22:17:59

How to make the character rotate in the direction of movement?

Hello, I’m making a game for android, I’m just starting to develop on unity, I want to turn the character in the direction of movement, because he constantly looks to the right, I need to turn left, but all in vain, I’ll attach the code that is on the character below. Management occurs by pressing the touch buttons on the screen. If possible, please describe in detail, in the form of a script it would be ideal.

my script:

using UnityEngine;

public class gg : MonoBehaviour
{
    [SerializeField] private float Speed;
    private float HorSpeed;
    [SerializeField] private float Imp;

    private bool isGround;
    private Animator anim;
    

    Rigidbody2D rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        transform.Translate(HorSpeed, 0, 0);
        if (HorSpeed == 0)
        {
            anim.SetBool("running", false);
        }
        else
        {
            anim.SetBool("running", true);
        }
    
        
    
    }

    public void OnRight()
    {
        HorSpeed = Speed;
    }

    public void OnLeft()
    {
        HorSpeed = -Speed;
    }

    public void OnJump()
    {
        if (isGround)
            rb.AddForce(new Vector2(0, Imp), ForceMode2D.Impulse);
    }

    public void Stop()
    {
        HorSpeed = 0;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Platform")
            isGround = true;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Platform")
            isGround = false;
    }



}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noytmant, 2021-08-02
@kovurado

In theory, you need to create a bool for each of the situations
. Let's say at the moment we have OnLeft and now we can create a bool PosLeft which will initially be false
At the moment when we click on OnLeft, let's say, PosLeft becomes true
And already inside a separate loop we check PosLeft based on which we set the position of the character
And this method can be used with other situations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question