Y
Y
Yura Mailler2020-08-05 16:23:15
C++ / C#
Yura Mailler, 2020-08-05 16:23:15

Character management in a 3D Android game?

Hello everyone, I'm making a game (FIRST) and I ran into such a problem that I need to make control.
I searched the entire Internet and found only a tutorial on 2D control, everything works, but for some reason my character model turns 180 degrees. I need to make sure that the character does not turn around and turns to the right and to the left ( I MADE BUTTONS TO CONTROL LEFT AND RIGHT

public Rigidbody rb;
    public float playerSpeed;
    public float jumpPower;
    public int directionInput;
    public bool groundCheck;
    public bool facingRight = true;


    void Start()
    {
        rb = GetComponent<Rigidbody>();

    }


    void Update()
    {
        if ((directionInput < 0) && (facingRight))
        {
            Flip();
        }

        if ((directionInput > 0) && (!facingRight))
        {
            Flip();
        }
        groundCheck = true;
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2(playerSpeed * directionInput, rb.velocity.y);
    }

    public void Move(int InputAxis)
    {

        directionInput = InputAxis;
       
    }

    public void Jump(bool isJump)
    {
        isJump = groundCheck;

        if (groundCheck)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpPower);
        }

    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }


}
) .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-08-05
@zZaKko

If you work with an object in which there are 3 vectors, then instead of new Vector2 you need to use new Vector3, because otherwise z will be reset.

rb.velocity = new Vector3(playerSpeed * directionInput, rb.velocity.y, rb.velocity.z);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question