Unity中使用C#脚本实现玩家前后左右移动
原创
于2026-01-05 18:07:00发布
12 阅读
0
0
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharcterMotor : MonoBehaviour { public float moveSpeed = 10; public float rotateSpeed = 50; private void Update() { float hor = Input.GetAxis("Horizontal"); float ver = Input.GetAxis("Vertical"); MovementRotateion(hor, ver); } private void MovementRotateion(float hor,float ver) { if (hor != 0 || ver != 0) { Quaternion dir = Quaternion.LookRotation(new Vector3(hor, 0, ver)); this.transform.rotation = Quaternion.Lerp(this.transform.rotation,dir,Time.deltaTime * rotateSpeed); this.transform.Translate(0, 0, -Time.deltaTime * moveSpeed); } } }