186 lines
5.2 KiB
C#
186 lines
5.2 KiB
C#
using UnityEngine;
|
|
|
|
namespace Invector.vCharacterController
|
|
{
|
|
public class vThirdPersonInput : MonoBehaviour
|
|
{
|
|
#region Variables
|
|
[Header("God")]
|
|
public bool God; //上帝模式
|
|
|
|
[Header("Controller Input")]
|
|
public string horizontalInput = "Horizontal";
|
|
public string verticallInput = "Vertical";
|
|
public KeyCode jumpInput = KeyCode.Space;
|
|
public KeyCode strafeInput = KeyCode.Tab;
|
|
public KeyCode sprintInput = KeyCode.LeftShift;
|
|
|
|
[Header("Camera Input")]
|
|
public string rotateCameraXInput = "Mouse X";
|
|
public string rotateCameraYInput = "Mouse Y";
|
|
|
|
[HideInInspector] public vThirdPersonController cc;
|
|
[HideInInspector] public vThirdPersonCamera tpCamera;
|
|
[HideInInspector] public Camera cameraMain;
|
|
|
|
public Joystick Joystick;
|
|
|
|
#endregion
|
|
|
|
protected virtual void Start()
|
|
{
|
|
Init();
|
|
}
|
|
public void Init()
|
|
{
|
|
InitilizeController();
|
|
InitializeTpCamera();
|
|
|
|
Joystick.JumpBtn.onClick.AddListener(() =>
|
|
{
|
|
if (God) return;
|
|
if (JumpConditions())
|
|
cc.Jump();
|
|
});
|
|
}
|
|
|
|
protected virtual void FixedUpdate()
|
|
{
|
|
if (God) return;
|
|
cc.UpdateMotor(); // updates the ThirdPersonMotor methods
|
|
cc.ControlLocomotionType(); // handle the controller locomotion type and movespeed
|
|
cc.ControlRotationType(); // handle the controller rotation type
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
InputHandle(); // update the input methods
|
|
cc.UpdateAnimator(); // updates the Animator Parameters
|
|
}
|
|
|
|
public virtual void OnAnimatorMove()
|
|
{
|
|
cc.ControlAnimatorRootMotion(); // handle root motion animations
|
|
}
|
|
|
|
#region Basic Locomotion Inputs
|
|
|
|
protected virtual void InitilizeController()
|
|
{
|
|
cc = GetComponent<vThirdPersonController>();
|
|
|
|
if (cc != null)
|
|
cc.Init();
|
|
}
|
|
|
|
protected virtual void InitializeTpCamera()
|
|
{
|
|
if (tpCamera == null)
|
|
{
|
|
tpCamera = FindObjectOfType<vThirdPersonCamera>();
|
|
if (tpCamera == null)
|
|
return;
|
|
if (tpCamera)
|
|
{
|
|
tpCamera.SetMainTarget(this.transform);
|
|
tpCamera.Init();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void InputHandle()
|
|
{
|
|
MoveInput();
|
|
CameraInput();
|
|
SprintInput();
|
|
StrafeInput();
|
|
JumpInput();
|
|
}
|
|
|
|
public virtual void MoveInput()
|
|
{
|
|
//cc.input.x = Input.GetAxis(horizontalInput);
|
|
//cc.input.z = Input.GetAxis(verticallInput);
|
|
|
|
float x = Input.GetAxis(horizontalInput);
|
|
float z = Input.GetAxis(verticallInput);
|
|
|
|
if (Joystick.gameObject.activeInHierarchy)
|
|
{
|
|
cc.input.x = x == 0 ? Joystick.Horizontal : x;
|
|
cc.input.z = z == 0 ? Joystick.Vertical : z;
|
|
}
|
|
else
|
|
{
|
|
cc.input.x = x;
|
|
cc.input.z = z;
|
|
}
|
|
}
|
|
|
|
protected virtual void CameraInput()
|
|
{
|
|
if (!cameraMain)
|
|
{
|
|
if (!Camera.main) Debug.Log("Missing a Camera with the tag MainCamera, please add one.");
|
|
else
|
|
{
|
|
cameraMain = Camera.main;
|
|
cc.rotateTarget = cameraMain.transform;
|
|
}
|
|
}
|
|
|
|
if (cameraMain)
|
|
{
|
|
cc.UpdateMoveDirection(cameraMain.transform);
|
|
}
|
|
|
|
if (tpCamera == null)
|
|
return;
|
|
|
|
var Y = Input.GetAxis(rotateCameraYInput);
|
|
var X = Input.GetAxis(rotateCameraXInput);
|
|
|
|
if (Input.GetKey(KeyCode.Mouse1))
|
|
{
|
|
//tpCamera.RotateCamera(X, Y);
|
|
}
|
|
}
|
|
|
|
protected virtual void StrafeInput()
|
|
{
|
|
return;
|
|
if (Input.GetKeyDown(strafeInput))
|
|
cc.Strafe();
|
|
}
|
|
|
|
protected virtual void SprintInput()
|
|
{
|
|
return;
|
|
if (Input.GetKeyDown(sprintInput))
|
|
cc.Sprint(true);
|
|
else if (Input.GetKeyUp(sprintInput))
|
|
cc.Sprint(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conditions to trigger the Jump animation & behavior
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected virtual bool JumpConditions()
|
|
{
|
|
return cc.isGrounded && cc.GroundAngle() < cc.slopeLimit && !cc.isJumping && !cc.stopMove;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Input to trigger the Jump
|
|
/// </summary>
|
|
protected virtual void JumpInput()
|
|
{
|
|
if (God) return;
|
|
if (Input.GetKeyDown(jumpInput) && JumpConditions())
|
|
cc.Jump();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |