Unity添加键盘控制代码

public class ArrowControl : MonoBehaviour
{
    public float moveSpeed = 5f;
    // 实例化两个 Cube 并赋予初始位置
    //cube1 = Instantiate(cubePrefab, new Vector3(-2f, 0.5f, 0f), Quaternion.identity);
    //cube2 = Instantiate(cubePrefab, new Vector3(2f, 0.5f, 0f), Quaternion.identity);

    // 添加箭头脚本以控制移动
    //cube1.AddComponent<ArrowControl>();
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontal, 0f, vertical) * moveSpeed * Time.deltaTime;
        transform.Translate(movement);
    }
}

 

posted @ 2023-06-19 16:01  多见多闻  阅读(99)  评论(0)    收藏  举报