📜  如何在统一 2d 游戏中移动角色 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:04.055000             🧑  作者: Mango

代码示例1
using UnityEngine;

public class PlayerMovement : MonoBehaviour  // PlayerMovement is the name of the script
{
    public float speed = 10;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector2 pos = transform.position;

        pos.x += h * Time.deltaTime * speed;
        pos.y += v * Time.deltaTime * speed;

        transform.position = pos;
    }
}