WASD键控制物体移动

脚本挂在物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class write : MonoBehaviour {
private Transform m_Transform;
// Use this for initialization
void Start () {

    m_Transform = gameObject.GetComponent<Transform>();
}

// Update is called once per frame
void Update () {
另一种方法:
    //float h = Input.GetAxis("Horizontal");
    //float v = Input.GetAxis("Vertical");
    //transform.Translate(new Vector3(h, v, 0) * 2 * Time.deltaTime);

    MoveControl();
}
void MoveControl()
{
    if (Input.GetKey(KeyCode.W))
    {
        m_Transform.Translate(Vector3.forward * 0.1f, Space.Self);
    }

    if (Input.GetKey(KeyCode.S))
    {
        m_Transform.Translate(Vector3.back * 0.1f, Space.Self);
    }

    if (Input.GetKey(KeyCode.A))
    {
        m_Transform.Translate(Vector3.left * 0.1f, Space.Self);
    }

    if (Input.GetKey(KeyCode.D))
    {
        m_Transform.Translate(Vector3.right * 0.1f, Space.Self);
    }

    if (Input.GetKey(KeyCode.Q))
    {
        m_Transform.Rotate(Vector3.up, -1.0f);
    }

    if (Input.GetKey(KeyCode.E))
    {
        m_Transform.Rotate(Vector3.up, 1.0f);
    }
  }

}

猜你喜欢

转载自blog.csdn.net/qq_42986916/article/details/81742689