G
G
Gruntek2020-10-09 17:09:13
C++ / C#
Gruntek, 2020-10-09 17:09:13

The character in the game moves to the right of the specified point. What to do?

5f806e546e875087941545.png
this is what is originally.
5f806e6e009ef544452009.png
this is what becomes.
I'm a newbie myself, so if it's a stupid question, don't hit me.

In general, I tried to make a flip, but when I turned to the left, the character also turned and bounced a few cells.
Script:

public ControlTipe controlTipe;
    public Joystick joystick;
    public float speed;
    public Animator anim;
    public enum ControlTipe {PC,Android}
    private Rigidbody2D rb;
    private Vector2 moveInput;
    private Vector2 moveVelocity;
    private bool facingRight = true;

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

    void Start()
    {
        if (controlTipe == ControlTipe.PC)
        {
            joystick.gameObject.SetActive(false);
        }
    }
    
    void Update()
    {
        if (controlTipe == ControlTipe.PC)
            moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        if (controlTipe == ControlTipe.Android)
            moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
        moveVelocity = moveInput.normalized * speed;
        if (moveInput.x == 0)
        {
            anim.SetBool("isRuning", false);
        }
        else
        {
            anim.SetBool("isRuning", true);
        }

        if (!facingRight && moveInput.x > 0)
                Flip();
        if (facingRight && moveInput.x < 0)
                Flip();
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    }

    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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question