Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.4 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PacmanControls : MonoBehaviour
{
    float moveSpeed = 5, animationSpeed;

    Vector2 moveDirection;
    void Update()
    {
        HandleMovement();
        HandleControls();
    }

    void HandleControls()
    {
        if (Input.anyKeyDown)
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                moveDirection = Vector2.left;
                transform.eulerAngles = new Vector3(0, 0, 90);
            }
            else if (Input.GetKeyDown(KeyCode.D))
            {
                moveDirection = Vector2.right;
                transform.eulerAngles = new Vector3(0, 0, -90);
            }
            else if (Input.GetKeyDown(KeyCode.W))
            {
                moveDirection = Vector2.up;
                transform.eulerAngles = new Vector3(0, 0, 0);
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                moveDirection = Vector2.down;
                transform.eulerAngles = new Vector3(0, 0, 180);
            }
        }
    }
    void HandleMovement()
    {
        transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
        Debug.Log($" Move direction: {moveDirection}, End Vector: {moveDirection * moveSpeed * Time.deltaTime}");
    }
}
Leave a Comment