🚀

[Unity] プレイヤーの移動スクリプト

2023/07/27に公開
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasicMovement : MonoBehaviour
{
    float speed = 3.0f;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start");
    }

    // Update is called once per frame
    void Update()
    {
        // rotate
        if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
        {
            transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal"), 0));
        }
        // move forward
        if (Input.GetKey(KeyCode.W))
        {
            transform.position -= speed * transform.forward * Time.deltaTime;
        }
        // move backward
        if (Input.GetKey(KeyCode.S))
        {
            transform.position += speed * transform.forward * Time.deltaTime;
        }
    }
}

Discussion