新输入系统
This commit is contained in:
@@ -3,338 +3,336 @@ using UnityStandardAssets.Utility;
|
||||
|
||||
namespace UnityStandardAssets.Characters.FirstPerson
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private bool m_IsWalking;
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private bool m_IsWalking;
|
||||
|
||||
[SerializeField]
|
||||
public float m_WalkSpeed;
|
||||
[SerializeField] public float m_WalkSpeed;
|
||||
|
||||
[SerializeField]
|
||||
public float m_RunSpeed;
|
||||
[SerializeField] public float m_RunSpeed;
|
||||
|
||||
[SerializeField]
|
||||
[Range(0f, 1f)]
|
||||
private float m_RunstepLenghten;
|
||||
[SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
|
||||
|
||||
[SerializeField]
|
||||
private float m_JumpSpeed;
|
||||
[SerializeField] private float m_JumpSpeed;
|
||||
|
||||
[SerializeField]
|
||||
private float m_StickToGroundForce;
|
||||
[SerializeField] private float m_StickToGroundForce;
|
||||
|
||||
[SerializeField]
|
||||
private float m_GravityMultiplier;
|
||||
[SerializeField] private float m_GravityMultiplier;
|
||||
|
||||
[SerializeField]
|
||||
public MouseLook m_MouseLook;
|
||||
[SerializeField] public MouseLook m_MouseLook;
|
||||
|
||||
[SerializeField]
|
||||
private bool m_UseFovKick;
|
||||
[SerializeField] private bool m_UseFovKick;
|
||||
|
||||
[SerializeField]
|
||||
private FOVKick m_FovKick = new FOVKick();
|
||||
[SerializeField] private FOVKick m_FovKick = new FOVKick();
|
||||
|
||||
[SerializeField]
|
||||
private bool m_UseHeadBob;
|
||||
[SerializeField] private bool m_UseHeadBob;
|
||||
|
||||
[SerializeField]
|
||||
private CurveControlledBob m_HeadBob = new CurveControlledBob();
|
||||
[SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
|
||||
|
||||
[SerializeField]
|
||||
private LerpControlledBob m_JumpBob = new LerpControlledBob();
|
||||
[SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
|
||||
|
||||
[SerializeField]
|
||||
private float m_StepInterval;
|
||||
[SerializeField] private float m_StepInterval;
|
||||
|
||||
[SerializeField]
|
||||
private AudioClip[] m_FootstepSounds;
|
||||
[SerializeField] private AudioClip[] m_FootstepSounds;
|
||||
|
||||
[SerializeField]
|
||||
private AudioClip[] m_FootstepSoundsBridge;
|
||||
[SerializeField] private AudioClip[] m_FootstepSoundsBridge;
|
||||
|
||||
[SerializeField]
|
||||
private AudioClip[] m_FootstepSoundsStone;
|
||||
[SerializeField] private AudioClip[] m_FootstepSoundsStone;
|
||||
|
||||
[SerializeField]
|
||||
private AudioClip m_JumpSound;
|
||||
[SerializeField] private AudioClip m_JumpSound;
|
||||
|
||||
[SerializeField]
|
||||
private AudioClip m_LandSound;
|
||||
[SerializeField] private AudioClip m_LandSound;
|
||||
|
||||
private Camera m_Camera;
|
||||
private Camera m_Camera;
|
||||
|
||||
private bool m_Jump;
|
||||
private bool m_Jump;
|
||||
|
||||
public bool frezzeRotation;
|
||||
public bool frezzeRotation;
|
||||
|
||||
public bool frezzePosition;
|
||||
public bool frezzePosition;
|
||||
|
||||
public bool rotateMouseInFixedUpdate = true;
|
||||
public bool rotateMouseInFixedUpdate = true;
|
||||
|
||||
public Vector2 m_Input;
|
||||
public Vector2 m_Input;
|
||||
|
||||
public Vector3 m_MoveDir = Vector3.zero;
|
||||
public Vector3 m_MoveDir = Vector3.zero;
|
||||
|
||||
public CharacterController m_CharacterController;
|
||||
public CharacterController m_CharacterController;
|
||||
|
||||
private CollisionFlags m_CollisionFlags;
|
||||
private CollisionFlags m_CollisionFlags;
|
||||
|
||||
private bool m_PreviouslyGrounded;
|
||||
private bool m_PreviouslyGrounded;
|
||||
|
||||
private Vector3 m_OriginalCameraPosition;
|
||||
private Vector3 m_OriginalCameraPosition;
|
||||
|
||||
private float m_StepCycle;
|
||||
private float m_StepCycle;
|
||||
|
||||
private float m_NextStep;
|
||||
private float m_NextStep;
|
||||
|
||||
private bool m_Jumping;
|
||||
private bool m_Jumping;
|
||||
|
||||
private AudioSource m_AudioSource;
|
||||
private AudioSource m_AudioSource;
|
||||
|
||||
private string groundTypeTag = "";
|
||||
private string groundTypeTag = "";
|
||||
|
||||
public float horizontal;
|
||||
public float horizontal;
|
||||
|
||||
public float vertical;
|
||||
public float vertical;
|
||||
|
||||
public bool isJumping;
|
||||
public bool isJumping;
|
||||
|
||||
public bool isRuning;
|
||||
public bool isRuning;
|
||||
|
||||
public bool notMove;
|
||||
public bool notMove;
|
||||
|
||||
[HideInInspector]
|
||||
public Vector3 lastValidPosition = Vector3.zero;
|
||||
[HideInInspector] public Vector3 lastValidPosition = Vector3.zero;
|
||||
|
||||
public bool isWater;
|
||||
public bool isWater;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_CharacterController = GetComponent<CharacterController>();
|
||||
m_Camera = Camera.main;
|
||||
m_OriginalCameraPosition = m_Camera.transform.localPosition;
|
||||
m_FovKick.Setup(m_Camera);
|
||||
m_HeadBob.Setup(m_Camera, m_StepInterval);
|
||||
m_StepCycle = 0f;
|
||||
m_NextStep = m_StepCycle / 2f;
|
||||
m_Jumping = false;
|
||||
m_AudioSource = GetComponent<AudioSource>();
|
||||
m_MouseLook.Init(transform, m_Camera.transform);
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
m_CharacterController = GetComponent<CharacterController>();
|
||||
m_Camera = Camera.main;
|
||||
m_OriginalCameraPosition = m_Camera.transform.localPosition;
|
||||
m_FovKick.Setup(m_Camera);
|
||||
m_HeadBob.Setup(m_Camera, m_StepInterval);
|
||||
m_StepCycle = 0f;
|
||||
m_NextStep = m_StepCycle / 2f;
|
||||
m_Jumping = false;
|
||||
m_AudioSource = GetComponent<AudioSource>();
|
||||
m_MouseLook.Init(transform, m_Camera.transform);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!frezzeRotation && !rotateMouseInFixedUpdate)
|
||||
{
|
||||
RotateView();
|
||||
}
|
||||
if (!m_Jump)
|
||||
{
|
||||
m_Jump = isJumping;
|
||||
}
|
||||
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
|
||||
{
|
||||
StartCoroutine(m_JumpBob.DoBobCycle());
|
||||
PlayLandingSound();
|
||||
m_MoveDir.y = 0f;
|
||||
m_Jumping = false;
|
||||
}
|
||||
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
|
||||
{
|
||||
m_MoveDir.y = 0f;
|
||||
}
|
||||
m_PreviouslyGrounded = m_CharacterController.isGrounded;
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (!frezzeRotation && !rotateMouseInFixedUpdate)
|
||||
{
|
||||
RotateView();
|
||||
}
|
||||
|
||||
private void PlayLandingSound()
|
||||
{
|
||||
m_AudioSource.clip = m_LandSound;
|
||||
m_AudioSource.Play();
|
||||
m_NextStep = m_StepCycle + 0.5f;
|
||||
}
|
||||
if (!m_Jump)
|
||||
{
|
||||
m_Jump = isJumping;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!frezzeRotation && rotateMouseInFixedUpdate)
|
||||
{
|
||||
RotateView();
|
||||
}
|
||||
if (frezzePosition)
|
||||
{
|
||||
return;
|
||||
}
|
||||
GetInput(out var speed);
|
||||
Vector3 vector = transform.forward * m_Input.y + transform.right * m_Input.x;
|
||||
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out var hitInfo, m_CharacterController.height / 2f, -1, QueryTriggerInteraction.Ignore);
|
||||
vector = Vector3.ProjectOnPlane(vector, hitInfo.normal).normalized;
|
||||
m_MoveDir.x = vector.x * speed;
|
||||
m_MoveDir.z = vector.z * speed;
|
||||
if (m_CharacterController.isGrounded)
|
||||
{
|
||||
m_MoveDir.y = 0f - m_StickToGroundForce;
|
||||
if (m_Jump && !isWater)
|
||||
{
|
||||
if ((bool)transform.parent && transform.parent.gameObject.layer == LayerMask.NameToLayer("Boat"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_MoveDir.y = m_JumpSpeed;
|
||||
PlayJumpSound();
|
||||
m_Jump = false;
|
||||
m_Jumping = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_MoveDir += Physics.gravity * m_GravityMultiplier * Time.fixedDeltaTime;
|
||||
}
|
||||
if (!notMove)
|
||||
{
|
||||
m_CollisionFlags = m_CharacterController.Move(m_MoveDir * Time.fixedDeltaTime);
|
||||
}
|
||||
ProgressStepCycle(speed);
|
||||
UpdateCameraPosition(speed);
|
||||
m_MouseLook.UpdateCursorLock();
|
||||
}
|
||||
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
|
||||
{
|
||||
StartCoroutine(m_JumpBob.DoBobCycle());
|
||||
PlayLandingSound();
|
||||
m_MoveDir.y = 0f;
|
||||
m_Jumping = false;
|
||||
}
|
||||
|
||||
private void PlayJumpSound()
|
||||
{
|
||||
m_AudioSource.clip = m_JumpSound;
|
||||
m_AudioSource.Play();
|
||||
}
|
||||
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
|
||||
{
|
||||
m_MoveDir.y = 0f;
|
||||
}
|
||||
|
||||
private void ProgressStepCycle(float speed)
|
||||
{
|
||||
if (m_CharacterController.velocity.sqrMagnitude > 0f && (m_Input.x != 0f || m_Input.y != 0f))
|
||||
{
|
||||
m_StepCycle += (m_CharacterController.velocity.magnitude + speed * (m_IsWalking ? 1f : m_RunstepLenghten)) * Time.fixedDeltaTime;
|
||||
}
|
||||
if (m_StepCycle > m_NextStep)
|
||||
{
|
||||
m_NextStep = m_StepCycle + m_StepInterval;
|
||||
PlayFootStepAudio();
|
||||
}
|
||||
}
|
||||
m_PreviouslyGrounded = m_CharacterController.isGrounded;
|
||||
}
|
||||
|
||||
private void PlayFootStepAudio()
|
||||
{
|
||||
if (!m_CharacterController.isGrounded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (groundTypeTag == "Bridge" || groundTypeTag == "Boat")
|
||||
{
|
||||
if (m_FootstepSoundsBridge.Length != 0)
|
||||
{
|
||||
int num = Random.Range(1, m_FootstepSoundsBridge.Length);
|
||||
m_AudioSource.clip = m_FootstepSoundsBridge[num];
|
||||
m_AudioSource.PlayOneShot(m_AudioSource.clip, 0.3f);
|
||||
m_FootstepSoundsBridge[num] = m_FootstepSoundsBridge[0];
|
||||
m_FootstepSoundsBridge[0] = m_AudioSource.clip;
|
||||
}
|
||||
}
|
||||
else if (groundTypeTag == "Stone")
|
||||
{
|
||||
if (m_FootstepSoundsStone.Length != 0)
|
||||
{
|
||||
int num2 = Random.Range(1, m_FootstepSoundsStone.Length);
|
||||
m_AudioSource.clip = m_FootstepSoundsStone[num2];
|
||||
m_AudioSource.PlayOneShot(m_AudioSource.clip, 0.3f);
|
||||
m_FootstepSoundsStone[num2] = m_FootstepSoundsStone[0];
|
||||
m_FootstepSoundsStone[0] = m_AudioSource.clip;
|
||||
}
|
||||
}
|
||||
else if (m_FootstepSounds.Length != 0)
|
||||
{
|
||||
int num3 = Random.Range(1, m_FootstepSounds.Length);
|
||||
m_AudioSource.clip = m_FootstepSounds[num3];
|
||||
m_AudioSource.PlayOneShot(m_AudioSource.clip, 0.3f);
|
||||
m_FootstepSounds[num3] = m_FootstepSounds[0];
|
||||
m_FootstepSounds[0] = m_AudioSource.clip;
|
||||
}
|
||||
}
|
||||
private void PlayLandingSound()
|
||||
{
|
||||
m_AudioSource.clip = m_LandSound;
|
||||
m_AudioSource.Play();
|
||||
m_NextStep = m_StepCycle + 0.5f;
|
||||
}
|
||||
|
||||
private void UpdateCameraPosition(float speed)
|
||||
{
|
||||
if (m_UseHeadBob)
|
||||
{
|
||||
Vector3 localPosition;
|
||||
if (m_CharacterController.velocity.magnitude > 0f && m_CharacterController.isGrounded)
|
||||
{
|
||||
m_Camera.transform.localPosition = m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude + speed * (m_IsWalking ? 1f : m_RunstepLenghten));
|
||||
localPosition = m_Camera.transform.localPosition;
|
||||
localPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
|
||||
}
|
||||
else
|
||||
{
|
||||
localPosition = m_Camera.transform.localPosition;
|
||||
localPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
|
||||
}
|
||||
m_Camera.transform.localPosition = localPosition;
|
||||
}
|
||||
}
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!frezzeRotation && rotateMouseInFixedUpdate)
|
||||
{
|
||||
RotateView();
|
||||
}
|
||||
|
||||
private void GetInput(out float speed)
|
||||
{
|
||||
bool isWalking = m_IsWalking;
|
||||
m_IsWalking = !isRuning;
|
||||
speed = (m_IsWalking ? m_WalkSpeed : m_RunSpeed);
|
||||
m_Input = new Vector2(horizontal, vertical);
|
||||
if (m_Input.sqrMagnitude > 1f)
|
||||
{
|
||||
m_Input.Normalize();
|
||||
}
|
||||
if (m_IsWalking != isWalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0f)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine((!m_IsWalking) ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
|
||||
}
|
||||
}
|
||||
if (frezzePosition)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public void RotateView()
|
||||
{
|
||||
m_MouseLook.LookRotation(transform, m_Camera.transform);
|
||||
}
|
||||
GetInput(out var speed);
|
||||
Vector3 vector = transform.forward * m_Input.y + transform.right * m_Input.x;
|
||||
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out var hitInfo,
|
||||
m_CharacterController.height / 2f, -1, QueryTriggerInteraction.Ignore);
|
||||
vector = Vector3.ProjectOnPlane(vector, hitInfo.normal).normalized;
|
||||
m_MoveDir.x = vector.x * speed;
|
||||
m_MoveDir.z = vector.z * speed;
|
||||
if (m_CharacterController.isGrounded)
|
||||
{
|
||||
m_MoveDir.y = 0f - m_StickToGroundForce;
|
||||
if (m_Jump && !isWater)
|
||||
{
|
||||
if ((bool)transform.parent && transform.parent.gameObject.layer == LayerMask.NameToLayer("Boat"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public void UnFrezzeLook()
|
||||
{
|
||||
frezzePosition = false;
|
||||
frezzeRotation = false;
|
||||
m_MouseLook.Init(transform, m_Camera.transform);
|
||||
}
|
||||
m_MoveDir.y = m_JumpSpeed;
|
||||
PlayJumpSound();
|
||||
m_Jump = false;
|
||||
m_Jumping = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_MoveDir += Physics.gravity * m_GravityMultiplier * Time.fixedDeltaTime;
|
||||
}
|
||||
|
||||
private void OnControllerColliderHit(ControllerColliderHit hit)
|
||||
{
|
||||
Rigidbody attachedRigidbody = hit.collider.attachedRigidbody;
|
||||
groundTypeTag = hit.collider.tag;
|
||||
if (!isWater && (groundTypeTag == "Terrain" || groundTypeTag == "Boat"))
|
||||
{
|
||||
lastValidPosition = transform.position;
|
||||
}
|
||||
if (m_CollisionFlags != CollisionFlags.Below && !(attachedRigidbody == null))
|
||||
{
|
||||
_ = attachedRigidbody.isKinematic;
|
||||
}
|
||||
}
|
||||
if (!notMove)
|
||||
{
|
||||
m_CollisionFlags = m_CharacterController.Move(m_MoveDir * Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
if (other.tag == "Water")
|
||||
{
|
||||
isWater = true;
|
||||
}
|
||||
}
|
||||
ProgressStepCycle(speed);
|
||||
UpdateCameraPosition(speed);
|
||||
m_MouseLook.UpdateCursorLock();
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (other.tag == "Water")
|
||||
{
|
||||
isWater = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void PlayJumpSound()
|
||||
{
|
||||
m_AudioSource.clip = m_JumpSound;
|
||||
m_AudioSource.Play();
|
||||
}
|
||||
|
||||
private void ProgressStepCycle(float speed)
|
||||
{
|
||||
if (m_CharacterController.velocity.sqrMagnitude > 0f && (m_Input.x != 0f || m_Input.y != 0f))
|
||||
{
|
||||
m_StepCycle +=
|
||||
(m_CharacterController.velocity.magnitude + speed * (m_IsWalking ? 1f : m_RunstepLenghten)) *
|
||||
Time.fixedDeltaTime;
|
||||
}
|
||||
|
||||
if (m_StepCycle > m_NextStep)
|
||||
{
|
||||
m_NextStep = m_StepCycle + m_StepInterval;
|
||||
PlayFootStepAudio();
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayFootStepAudio()
|
||||
{
|
||||
if (!m_CharacterController.isGrounded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (groundTypeTag == "Bridge" || groundTypeTag == "Boat")
|
||||
{
|
||||
if (m_FootstepSoundsBridge.Length != 0)
|
||||
{
|
||||
int num = Random.Range(1, m_FootstepSoundsBridge.Length);
|
||||
m_AudioSource.clip = m_FootstepSoundsBridge[num];
|
||||
m_AudioSource.PlayOneShot(m_AudioSource.clip, 0.3f);
|
||||
m_FootstepSoundsBridge[num] = m_FootstepSoundsBridge[0];
|
||||
m_FootstepSoundsBridge[0] = m_AudioSource.clip;
|
||||
}
|
||||
}
|
||||
else if (groundTypeTag == "Stone")
|
||||
{
|
||||
if (m_FootstepSoundsStone.Length != 0)
|
||||
{
|
||||
int num2 = Random.Range(1, m_FootstepSoundsStone.Length);
|
||||
m_AudioSource.clip = m_FootstepSoundsStone[num2];
|
||||
m_AudioSource.PlayOneShot(m_AudioSource.clip, 0.3f);
|
||||
m_FootstepSoundsStone[num2] = m_FootstepSoundsStone[0];
|
||||
m_FootstepSoundsStone[0] = m_AudioSource.clip;
|
||||
}
|
||||
}
|
||||
else if (m_FootstepSounds.Length != 0)
|
||||
{
|
||||
int num3 = Random.Range(1, m_FootstepSounds.Length);
|
||||
m_AudioSource.clip = m_FootstepSounds[num3];
|
||||
m_AudioSource.PlayOneShot(m_AudioSource.clip, 0.3f);
|
||||
m_FootstepSounds[num3] = m_FootstepSounds[0];
|
||||
m_FootstepSounds[0] = m_AudioSource.clip;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateCameraPosition(float speed)
|
||||
{
|
||||
if (m_UseHeadBob)
|
||||
{
|
||||
Vector3 localPosition;
|
||||
if (m_CharacterController.velocity.magnitude > 0f && m_CharacterController.isGrounded)
|
||||
{
|
||||
m_Camera.transform.localPosition = m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
|
||||
speed *
|
||||
(m_IsWalking ? 1f : m_RunstepLenghten));
|
||||
localPosition = m_Camera.transform.localPosition;
|
||||
localPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
|
||||
}
|
||||
else
|
||||
{
|
||||
localPosition = m_Camera.transform.localPosition;
|
||||
localPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
|
||||
}
|
||||
|
||||
m_Camera.transform.localPosition = localPosition;
|
||||
}
|
||||
}
|
||||
|
||||
private void GetInput(out float speed)
|
||||
{
|
||||
bool isWalking = m_IsWalking;
|
||||
m_IsWalking = !isRuning;
|
||||
speed = (m_IsWalking ? m_WalkSpeed : m_RunSpeed);
|
||||
m_Input = new Vector2(horizontal, vertical);
|
||||
if (m_Input.sqrMagnitude > 1f)
|
||||
{
|
||||
m_Input.Normalize();
|
||||
}
|
||||
|
||||
if (m_IsWalking != isWalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0f)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine((!m_IsWalking) ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
|
||||
}
|
||||
}
|
||||
|
||||
public void RotateView()
|
||||
{
|
||||
m_MouseLook.LookRotation(transform, m_Camera.transform);
|
||||
}
|
||||
|
||||
public void UnFrezzeLook()
|
||||
{
|
||||
frezzePosition = false;
|
||||
frezzeRotation = false;
|
||||
m_MouseLook.Init(transform, m_Camera.transform);
|
||||
}
|
||||
|
||||
private void OnControllerColliderHit(ControllerColliderHit hit)
|
||||
{
|
||||
Rigidbody attachedRigidbody = hit.collider.attachedRigidbody;
|
||||
groundTypeTag = hit.collider.tag;
|
||||
if (!isWater && (groundTypeTag == "Terrain" || groundTypeTag == "Boat"))
|
||||
{
|
||||
lastValidPosition = transform.position;
|
||||
}
|
||||
|
||||
if (m_CollisionFlags != CollisionFlags.Below && !(attachedRigidbody == null))
|
||||
{
|
||||
_ = attachedRigidbody.isKinematic;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
if (other.CompareTag("Water"))
|
||||
{
|
||||
isWater = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (other.CompareTag("Water"))
|
||||
{
|
||||
isWater = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityStandardAssets.Utility;
|
||||
|
||||
namespace UnityStandardAssets.Characters.FirstPerson
|
||||
{
|
||||
public class HeadBob : MonoBehaviour
|
||||
{
|
||||
public Camera Camera;
|
||||
|
||||
public CurveControlledBob motionBob = new CurveControlledBob();
|
||||
|
||||
public LerpControlledBob jumpAndLandingBob = new LerpControlledBob();
|
||||
|
||||
public RigidbodyFirstPersonController rigidbodyFirstPersonController;
|
||||
|
||||
public float StrideInterval;
|
||||
|
||||
[Range(0f, 1f)]
|
||||
public float RunningStrideLengthen;
|
||||
|
||||
private bool m_PreviouslyGrounded;
|
||||
|
||||
private Vector3 m_OriginalCameraPosition;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
motionBob.Setup(Camera, StrideInterval);
|
||||
m_OriginalCameraPosition = Camera.transform.localPosition;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Vector3 localPosition;
|
||||
if (rigidbodyFirstPersonController.Velocity.magnitude > 0f && rigidbodyFirstPersonController.Grounded)
|
||||
{
|
||||
Camera.transform.localPosition = motionBob.DoHeadBob(rigidbodyFirstPersonController.Velocity.magnitude * (rigidbodyFirstPersonController.Running ? RunningStrideLengthen : 1f));
|
||||
localPosition = Camera.transform.localPosition;
|
||||
localPosition.y = Camera.transform.localPosition.y - jumpAndLandingBob.Offset();
|
||||
}
|
||||
else
|
||||
{
|
||||
localPosition = Camera.transform.localPosition;
|
||||
localPosition.y = m_OriginalCameraPosition.y - jumpAndLandingBob.Offset();
|
||||
}
|
||||
Camera.transform.localPosition = localPosition;
|
||||
if (!m_PreviouslyGrounded && rigidbodyFirstPersonController.Grounded)
|
||||
{
|
||||
StartCoroutine(jumpAndLandingBob.DoBobCycle());
|
||||
}
|
||||
m_PreviouslyGrounded = rigidbodyFirstPersonController.Grounded;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39e4175b0b333c6c1d9b6b5db831f603
|
||||
timeCreated: 1716587613
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
externalObjects: {}
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,122 +1,133 @@
|
||||
using System;
|
||||
using NBF;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityStandardAssets.Characters.FirstPerson
|
||||
{
|
||||
[Serializable]
|
||||
public class MouseLook
|
||||
{
|
||||
public float XSensitivity = 2f;
|
||||
[Serializable]
|
||||
public class MouseLook
|
||||
{
|
||||
public float XSensitivity = 2f;
|
||||
|
||||
public float YSensitivity = 2f;
|
||||
public float YSensitivity = 2f;
|
||||
|
||||
public bool clampVerticalRotation = true;
|
||||
public bool clampVerticalRotation = true;
|
||||
|
||||
public float MinimumX = -90f;
|
||||
public float MinimumX = -90f;
|
||||
|
||||
public float MaximumX = 90f;
|
||||
public float MaximumX = 90f;
|
||||
|
||||
public bool smooth;
|
||||
public bool smooth;
|
||||
|
||||
public float smoothTime = 5f;
|
||||
public float smoothTime = 5f;
|
||||
|
||||
public bool lockCursor = true;
|
||||
public bool lockCursor = true;
|
||||
|
||||
public float RotYvalue;
|
||||
public float RotYvalue;
|
||||
|
||||
public bool invertMouseY;
|
||||
public bool invertMouseY;
|
||||
|
||||
public bool invertMouseX;
|
||||
public bool invertMouseX;
|
||||
|
||||
public int ControllerHandMode;
|
||||
public int ControllerHandMode;
|
||||
|
||||
private Quaternion m_CharacterTargetRot;
|
||||
private Quaternion m_CharacterTargetRot;
|
||||
|
||||
private Quaternion m_CameraTargetRot;
|
||||
private Quaternion m_CameraTargetRot;
|
||||
|
||||
private bool m_cursorIsLocked = true;
|
||||
private bool m_cursorIsLocked = true;
|
||||
|
||||
// private Player player;
|
||||
// private Player player;
|
||||
|
||||
public void Init(Transform character, Transform camera)
|
||||
{
|
||||
m_CharacterTargetRot = character.localRotation;
|
||||
m_CameraTargetRot = camera.localRotation;
|
||||
// player = ReInput.players.GetPlayer(0);
|
||||
}
|
||||
public void Init(Transform character, Transform camera)
|
||||
{
|
||||
m_CharacterTargetRot = character.localRotation;
|
||||
m_CameraTargetRot = camera.localRotation;
|
||||
// player = ReInput.players.GetPlayer(0);
|
||||
}
|
||||
|
||||
public void LookRotation(Transform character, Transform camera)
|
||||
{
|
||||
// if (!SRDebug.Instance.IsDebugPanelVisible && !SRDebug.Instance.IsDebugPanelVisible)
|
||||
// {
|
||||
// float num = Mathf.Clamp(player.GetAxis("cameraVerticalGamepad") * YSensitivity * 50f * Time.deltaTime + player.GetAxis("cameraVertical") * YSensitivity * 5f * Time.deltaTime, -10f, 10f);
|
||||
// float num2 = Mathf.Clamp(player.GetAxis("cameraHorizontalGamepad") * XSensitivity * 50f * Time.deltaTime + player.GetAxis("cameraHorizontal") * XSensitivity * 5f * Time.deltaTime, -10f, 10f);
|
||||
// if (invertMouseY)
|
||||
// {
|
||||
// num *= -1f;
|
||||
// }
|
||||
// if (invertMouseX)
|
||||
// {
|
||||
// num2 *= -1f;
|
||||
// }
|
||||
// RotYvalue = num2;
|
||||
// m_CharacterTargetRot *= Quaternion.Euler(0f, num2, 0f);
|
||||
// m_CameraTargetRot *= Quaternion.Euler(0f - num, 0f, 0f);
|
||||
// if (clampVerticalRotation)
|
||||
// {
|
||||
// m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
|
||||
// }
|
||||
// if (smooth)
|
||||
// {
|
||||
// character.localRotation = Quaternion.Slerp(character.localRotation, m_CharacterTargetRot, smoothTime * Time.deltaTime);
|
||||
// camera.localRotation = Quaternion.Slerp(camera.localRotation, m_CameraTargetRot, smoothTime * Time.deltaTime);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// character.localRotation = m_CharacterTargetRot;
|
||||
// camera.localRotation = m_CameraTargetRot;
|
||||
// }
|
||||
// UpdateCursorLock();
|
||||
// }
|
||||
}
|
||||
public void LookRotation(Transform character, Transform camera)
|
||||
{
|
||||
// if (!SRDebug.Instance.IsDebugPanelVisible && !SRDebug.Instance.IsDebugPanelVisible)
|
||||
{
|
||||
var lookInput = InputManager.GetLookInput();
|
||||
|
||||
public void SetCursorLock(bool value)
|
||||
{
|
||||
lockCursor = value;
|
||||
if (!lockCursor)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
float num = Mathf.Clamp(lookInput.y * YSensitivity * 5f * Time.deltaTime, -10f, 10f);
|
||||
float num2 =
|
||||
Mathf.Clamp(lookInput.x * XSensitivity * 5f * Time.deltaTime, -10f, 10f);
|
||||
|
||||
public void UpdateCursorLock()
|
||||
{
|
||||
if (lockCursor)
|
||||
{
|
||||
InternalLockUpdate();
|
||||
}
|
||||
}
|
||||
if (invertMouseY)
|
||||
{
|
||||
num *= -1f;
|
||||
}
|
||||
|
||||
private void InternalLockUpdate()
|
||||
{
|
||||
}
|
||||
if (invertMouseX)
|
||||
{
|
||||
num2 *= -1f;
|
||||
}
|
||||
|
||||
private Quaternion ClampRotationAroundXAxis(Quaternion q)
|
||||
{
|
||||
q.x /= q.w;
|
||||
q.y /= q.w;
|
||||
q.z /= q.w;
|
||||
q.w = 1f;
|
||||
float value = 114.59156f * Mathf.Atan(q.x);
|
||||
value = Mathf.Clamp(value, MinimumX, MaximumX);
|
||||
q.x = Mathf.Tan((float)Math.PI / 360f * value);
|
||||
return q;
|
||||
}
|
||||
}
|
||||
}
|
||||
RotYvalue = num2;
|
||||
m_CharacterTargetRot *= Quaternion.Euler(0f, num2, 0f);
|
||||
m_CameraTargetRot *= Quaternion.Euler(0f - num, 0f, 0f);
|
||||
if (clampVerticalRotation)
|
||||
{
|
||||
m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
|
||||
}
|
||||
|
||||
if (smooth)
|
||||
{
|
||||
character.localRotation = Quaternion.Slerp(character.localRotation, m_CharacterTargetRot,
|
||||
smoothTime * Time.deltaTime);
|
||||
camera.localRotation = Quaternion.Slerp(camera.localRotation, m_CameraTargetRot,
|
||||
smoothTime * Time.deltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
character.localRotation = m_CharacterTargetRot;
|
||||
camera.localRotation = m_CameraTargetRot;
|
||||
}
|
||||
|
||||
UpdateCursorLock();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCursorLock(bool value)
|
||||
{
|
||||
lockCursor = value;
|
||||
if (!lockCursor)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateCursorLock()
|
||||
{
|
||||
if (lockCursor)
|
||||
{
|
||||
InternalLockUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private void InternalLockUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
private Quaternion ClampRotationAroundXAxis(Quaternion q)
|
||||
{
|
||||
q.x /= q.w;
|
||||
q.y /= q.w;
|
||||
q.z /= q.w;
|
||||
q.w = 1f;
|
||||
float value = 114.59156f * Mathf.Atan(q.x);
|
||||
value = Mathf.Clamp(value, MinimumX, MaximumX);
|
||||
q.x = Mathf.Tan((float)Math.PI / 360f * value);
|
||||
return q;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,226 +0,0 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityStandardAssets.CrossPlatformInput;
|
||||
|
||||
namespace UnityStandardAssets.Characters.FirstPerson
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(CapsuleCollider))]
|
||||
public class RigidbodyFirstPersonController : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public class MovementSettings
|
||||
{
|
||||
public float ForwardSpeed = 8f;
|
||||
|
||||
public float BackwardSpeed = 4f;
|
||||
|
||||
public float StrafeSpeed = 4f;
|
||||
|
||||
public float RunMultiplier = 2f;
|
||||
|
||||
public KeyCode RunKey = KeyCode.LeftShift;
|
||||
|
||||
public float JumpForce = 30f;
|
||||
|
||||
public AnimationCurve SlopeCurveModifier = new AnimationCurve(new Keyframe(-90f, 1f), new Keyframe(0f, 1f), new Keyframe(90f, 0f));
|
||||
|
||||
[HideInInspector]
|
||||
public float CurrentTargetSpeed = 8f;
|
||||
|
||||
private bool m_Running;
|
||||
|
||||
public bool Running => m_Running;
|
||||
|
||||
public void UpdateDesiredTargetSpeed(Vector2 input)
|
||||
{
|
||||
if (!(input == Vector2.zero))
|
||||
{
|
||||
if (input.x > 0f || input.x < 0f)
|
||||
{
|
||||
CurrentTargetSpeed = StrafeSpeed;
|
||||
}
|
||||
if (input.y < 0f)
|
||||
{
|
||||
CurrentTargetSpeed = BackwardSpeed;
|
||||
}
|
||||
if (input.y > 0f)
|
||||
{
|
||||
CurrentTargetSpeed = ForwardSpeed;
|
||||
}
|
||||
if (Input.GetKey(RunKey))
|
||||
{
|
||||
CurrentTargetSpeed *= RunMultiplier;
|
||||
m_Running = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class AdvancedSettings
|
||||
{
|
||||
public float groundCheckDistance = 0.01f;
|
||||
|
||||
public float stickToGroundHelperDistance = 0.5f;
|
||||
|
||||
public float slowDownRate = 20f;
|
||||
|
||||
public bool airControl;
|
||||
|
||||
[Tooltip("set it to 0.1 or more if you get stuck in wall")]
|
||||
public float shellOffset;
|
||||
}
|
||||
|
||||
public Camera cam;
|
||||
|
||||
public MovementSettings movementSettings = new MovementSettings();
|
||||
|
||||
public MouseLook mouseLook = new MouseLook();
|
||||
|
||||
public AdvancedSettings advancedSettings = new AdvancedSettings();
|
||||
|
||||
private Rigidbody m_RigidBody;
|
||||
|
||||
private CapsuleCollider m_Capsule;
|
||||
|
||||
private float m_YRotation;
|
||||
|
||||
private Vector3 m_GroundContactNormal;
|
||||
|
||||
private bool m_Jump;
|
||||
|
||||
private bool m_PreviouslyGrounded;
|
||||
|
||||
private bool m_Jumping;
|
||||
|
||||
private bool m_IsGrounded;
|
||||
|
||||
public Vector3 Velocity => m_RigidBody.linearVelocity;
|
||||
|
||||
public bool Grounded => m_IsGrounded;
|
||||
|
||||
public bool Jumping => m_Jumping;
|
||||
|
||||
public bool Running => movementSettings.Running;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_RigidBody = GetComponent<Rigidbody>();
|
||||
m_Capsule = GetComponent<CapsuleCollider>();
|
||||
mouseLook.Init(transform, cam.transform);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
RotateView();
|
||||
if (CrossPlatformInputManager.GetButtonDown("Jump") && !m_Jump)
|
||||
{
|
||||
m_Jump = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
GroundCheck();
|
||||
Vector2 input = GetInput();
|
||||
if ((Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon) && (advancedSettings.airControl || m_IsGrounded))
|
||||
{
|
||||
Vector3 vector = cam.transform.forward * input.y + cam.transform.right * input.x;
|
||||
vector = Vector3.ProjectOnPlane(vector, m_GroundContactNormal).normalized;
|
||||
vector.x *= movementSettings.CurrentTargetSpeed;
|
||||
vector.z *= movementSettings.CurrentTargetSpeed;
|
||||
vector.y *= movementSettings.CurrentTargetSpeed;
|
||||
if (m_RigidBody.linearVelocity.sqrMagnitude < movementSettings.CurrentTargetSpeed * movementSettings.CurrentTargetSpeed)
|
||||
{
|
||||
m_RigidBody.AddForce(vector * SlopeMultiplier(), ForceMode.Impulse);
|
||||
}
|
||||
}
|
||||
if (m_IsGrounded)
|
||||
{
|
||||
m_RigidBody.linearDamping = 5f;
|
||||
if (m_Jump)
|
||||
{
|
||||
m_RigidBody.linearDamping = 0f;
|
||||
m_RigidBody.linearVelocity = new Vector3(m_RigidBody.linearVelocity.x, 0f, m_RigidBody.linearVelocity.z);
|
||||
m_RigidBody.AddForce(new Vector3(0f, movementSettings.JumpForce, 0f), ForceMode.Impulse);
|
||||
m_Jumping = true;
|
||||
}
|
||||
if (!m_Jumping && Mathf.Abs(input.x) < float.Epsilon && Mathf.Abs(input.y) < float.Epsilon && m_RigidBody.linearVelocity.magnitude < 1f)
|
||||
{
|
||||
m_RigidBody.Sleep();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_RigidBody.linearDamping = 0f;
|
||||
if (m_PreviouslyGrounded && !m_Jumping)
|
||||
{
|
||||
StickToGroundHelper();
|
||||
}
|
||||
}
|
||||
m_Jump = false;
|
||||
}
|
||||
|
||||
private float SlopeMultiplier()
|
||||
{
|
||||
float time = Vector3.Angle(m_GroundContactNormal, Vector3.up);
|
||||
return movementSettings.SlopeCurveModifier.Evaluate(time);
|
||||
}
|
||||
|
||||
private void StickToGroundHelper()
|
||||
{
|
||||
if (Physics.SphereCast(transform.position, m_Capsule.radius * (1f - advancedSettings.shellOffset), Vector3.down, out var hitInfo, m_Capsule.height / 2f - m_Capsule.radius + advancedSettings.stickToGroundHelperDistance, -1, QueryTriggerInteraction.Ignore) && Mathf.Abs(Vector3.Angle(hitInfo.normal, Vector3.up)) < 85f)
|
||||
{
|
||||
m_RigidBody.linearVelocity = Vector3.ProjectOnPlane(m_RigidBody.linearVelocity, hitInfo.normal);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 GetInput()
|
||||
{
|
||||
Vector2 vector = default(Vector2);
|
||||
vector.x = CrossPlatformInputManager.GetAxis("Horizontal");
|
||||
vector.y = CrossPlatformInputManager.GetAxis("Vertical");
|
||||
Vector2 vector2 = vector;
|
||||
movementSettings.UpdateDesiredTargetSpeed(vector2);
|
||||
return vector2;
|
||||
}
|
||||
|
||||
private void RotateView()
|
||||
{
|
||||
if (!(Mathf.Abs(Time.timeScale) < float.Epsilon))
|
||||
{
|
||||
float y = transform.eulerAngles.y;
|
||||
mouseLook.LookRotation(transform, cam.transform);
|
||||
if (m_IsGrounded || advancedSettings.airControl)
|
||||
{
|
||||
Quaternion quaternion = Quaternion.AngleAxis(transform.eulerAngles.y - y, Vector3.up);
|
||||
m_RigidBody.linearVelocity = quaternion * m_RigidBody.linearVelocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void GroundCheck()
|
||||
{
|
||||
m_PreviouslyGrounded = m_IsGrounded;
|
||||
if (Physics.SphereCast(transform.position, m_Capsule.radius * (1f - advancedSettings.shellOffset), Vector3.down, out var hitInfo, m_Capsule.height / 2f - m_Capsule.radius + advancedSettings.groundCheckDistance, -1, QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
m_IsGrounded = true;
|
||||
m_GroundContactNormal = hitInfo.normal;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_IsGrounded = false;
|
||||
m_GroundContactNormal = Vector3.up;
|
||||
}
|
||||
if (!m_PreviouslyGrounded && m_IsGrounded && m_Jumping)
|
||||
{
|
||||
m_Jumping = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3facfead7f1a8f983f2d3f5f1e3b3e48
|
||||
timeCreated: 1716587613
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
externalObjects: {}
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user