鍵盤控制方向前進,鼠標控制X Y軸方向

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour

{

? ? private Transform m_Transform;

? ? void Start()

? ? {

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

? ? }

? ? // Update is called once per frame

? ? void Update()

? ? {

? ? ? ? MoveControl();

? ? }

? ? void MoveControl()

? ? {

? ? ? ? if (Input.GetKey(KeyCode.W))? ? //鍵盤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);

? ? ? ? }

? ? ? m_Transform.Rotate(Vector3.up, Input.GetAxis("Mouse X"));? //鼠標控制X

? ? ? m_Transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y"));? //鼠標控制Y

? ? }

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容