参考:https://www.bilibili.com/video/BV1x741177tZ?from=search&seid=12277026178001217406
playermove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermove : MonoBehaviour {
public float speed;
// Update is called once per frame
public CharacterController playercontroller;
void Update ()
{
float x, z;
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
Vector3 move;
move = transform.right * x + transform.forward * z;
playercontroller.Move(move * speed * Time.deltaTime); ;
}
}
cameralook.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cameralook : MonoBehaviour {
public float mousespeed;
private float xmove;
public Transform player;
// Use this for initialization
void Start () {
Cursor.lockState = CursorLockMode.Locked;//锁定鼠标
}
// Update is called once per frame
void Update ()
{
float x, y;
x = Input.GetAxis("Mouse X")*mousespeed*Time.deltaTime;
y = Input.GetAxis("Mouse Y")*mousespeed*Time.deltaTime;
xmove = xmove - y;
xmove = Mathf.Clamp(xmove, -90, 90);//限制最大最小值
this.transform.localRotation = Quaternion.Euler(xmove,0,0);
player.Rotate(Vector3.up*x);
}
}