Untitled

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

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

    Vector2 moveDirection;
    // Start is called before the first frame update
    void Start()
    {
        HandleDirection("Up");
    }

    // Update is called once per frame
    void Update()
    {
        HandleMovement();
        HandleControls();
    }

    void HandleDirection(string input)
    {
        switch (input)
        {
            case "Right":
                moveDirection = Vector2.right;
                break;
            case "Left":
                moveDirection = Vector2.left;
                break;
            case "Up":
                moveDirection = Vector2.up;
                break;
            case "Down":
                moveDirection = Vector2.down;
                break;
            default:
                HandleDirection("Left");
                break;
        }
        HandleRotation(input);
    }

    void HandleControls()
    {
        string direction;
        if (Input.anyKeyDown)
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                direction = "Left";
            }
            else if (Input.GetKeyDown(KeyCode.D))
            {
                direction = "Right";
            }
            else if (Input.GetKeyDown(KeyCode.W))
            {
                direction = "Up";
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                direction = "Down";
            }
            else
            {
                return;
            }
            HandleDirection(direction);
        }
    }
    void HandleMovement()
    {
        transform.Translate(moveDirection * moveSpeed * Time.deltaTime);
        Debug.Log(moveDirection * moveSpeed * Time.deltaTime);
    }

    void HandleRotation(string input)
    {
        switch (input)
        {
            case "Right":
                transform.eulerAngles = new Vector3(0, 0, 0);
                break;
            case "Left":
                transform.eulerAngles = new Vector3(0, 0, 180);
                break;
            case "Up":
                transform.eulerAngles = new Vector3(0, 0, 90);
                break;
            case "Down":
                transform.eulerAngles = new Vector3(0, 0, -90);
                break;
            default:
                HandleRotation("Left");
                break;
        }
    }
}
Leave a Comment