V
V
vordid2018-10-27 20:26:58
C++ / C#
vordid, 2018-10-27 20:26:58

How to make it so that when the character goes to the left, he looks to the left, and similarly to the right in Unity, a 2D game?

Here is the character code:

public float moveSpeed = 8f;
    public Joystick joystick;

    private void Update()
    {
        Vector3 moveVector = (Vector3.right * joystick.Horizontal);

        if (moveVector != Vector3.zero)
        {
            transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
        }

Jostick:
Vector2 joystickPosition = Vector2.zero;
    private Camera cam = new Camera();

    void Start()
    {
        joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
    }

    public override void OnDrag(PointerEventData eventData)
    {
        Vector2 direction = eventData.position - joystickPosition;
        inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? 
        direction.normalized : direction / (background.sizeDelta.x / 2f);
        ClampJoystick();
        handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        OnDrag(eventData);
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        inputVector = Vector2.zero;
        handle.anchoredPosition = Vector2.zero;
    }
}

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