P
P
POP_MOB2019-12-11 12:43:26
C++ / C#
POP_MOB, 2019-12-11 12:43:26

Why doesn't Flip work on Unity3D?

Please help solve the problem with the Flip team. It does not work when the character is moving.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{

    public float speed;

    private Rigidbody2D rb;
    private Vector2 moveVelositi;
    private bool facingRight = true;
    private float moveInput;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        moveVelositi = moveInput.normalized * speed;
        
    }

    void FixedUpdate()
    {

        rb.MovePosition(rb.position + moveVelositi * Time.fixedDeltaTime);

        if (facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (facingRight == true && moveInput < 0)
        {
            Flip();
        }
    }

    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