V
V
Veros Game2021-05-01 11:26:29
C++ / C#
Veros Game, 2021-05-01 11:26:29

Top-Down 2D game. How can I make it look left when the character walks to the left?

Here is the script

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

public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody2D rb;
    private Vector2 moveVelocity;

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


    void Update() 
    {
        Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        moveVelocity = moveInput * speed;
        
    }

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

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Zaletsky, 2021-05-01
@JZ_52

if (Input.GetKey(KeyCode.A))
        {
            speedX = -horizontalSpeed;
            transform.localScale = new Vector3(-1, 1); //Персонаж смотри в ту сторону, куда нажал игрок

        }
        else if (Input.GetKey(KeyCode.D))
        {
            speedX = horizontalSpeed;
            transform.localScale = new Vector3(1, 1); //Персонаж смотри в ту сторону, куда нажал игрок
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question