玩家移动
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class FirstPerson : MonoBehaviour
{
public CharacterController player;
public float speed;
public float gravity = 9.8f ;
public Vector3 moveDirect;
public float jumpHeight;
private void Update ( )
{
if ( player. isGrounded)
{
float xMove = Input. GetAxisRaw ( "Horizontal" ) ;
float yMove = Input. GetAxisRaw ( "Vertical" ) ;
moveDirect = transform. TransformDirection ( new Vector3 ( xMove, 0 , yMove) ) ;
if ( Input. GetButtonDown ( "Jump" ) )
{
moveDirect. y = jumpHeight;
}
}
moveDirect. y -= gravity * Time. deltaTime;
player. Move ( speed * Time. deltaTime * moveDirect) ;
}
}
相机旋转
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class MouseLook : MonoBehaviour
{
public Transform player;
public float sensitivity = 2f ;
public Vector2 cameraRot;
public Vector2 angeleLimit;
void Update ( )
{
LookRotate ( ) ;
}
public void LookRotate ( )
{
float yRot = Input. GetAxis ( "Mouse X" ) * sensitivity;
float xRot = Input. GetAxis ( "Mouse Y" ) * sensitivity;
cameraRot. x += xRot;
cameraRot. y -= yRot;
cameraRot. x = Mathf. Clamp ( cameraRot. x, angeleLimit. x, angeleLimit. y) ;
transform. rotation = Quaternion. Euler ( cameraRot. x, cameraRot. y, 0 ) ;
player. rotation = Quaternion. Euler ( 0 , cameraRot. y, 0 ) ;
}
}