|
@@ -1,10 +1,11 @@
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.EventSystems;
|
|
|
+using static UnityEditor.ShaderGraph.Internal.KeywordDependentCollection;
|
|
|
|
|
|
public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
public PlayerInputComponent playerInput;
|
|
|
- public Transform camera;
|
|
|
+ public Transform camRoot;
|
|
|
public float jumpHeight = 2.0f;
|
|
|
public float gravity = -13.524f;
|
|
|
|
|
@@ -17,11 +18,16 @@ public class MoveController : MonoBehaviour
|
|
|
private float jumpTime = 0.5f;
|
|
|
private bool isGrounded = false;
|
|
|
private bool hasLand = false;
|
|
|
+ private bool isBlock = false;
|
|
|
|
|
|
private AnimatorController controller;
|
|
|
+ private Transform head;
|
|
|
+ private Transform m_camera;
|
|
|
private Vector3 move;
|
|
|
private Vector3 gravityMove;
|
|
|
private Vector3 lastMove;
|
|
|
+ private Vector3 camInitPos;
|
|
|
+ private RaycastHit hit;
|
|
|
private Vector3 colliderDir;
|
|
|
|
|
|
public enum PlayerState
|
|
@@ -39,10 +45,13 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
controller = GetComponent<AnimatorController>();
|
|
|
character = GetComponent<CharacterController>();
|
|
|
- playerInput = GetComponent<PlayerInputComponent>();
|
|
|
- angleX = Vector3.Angle(Vector3.right, camera.right);
|
|
|
- angleY = Vector3.Angle(Vector3.up, camera.up);
|
|
|
+ playerInput=GetComponent<PlayerInputComponent>();
|
|
|
+ angleX = Vector3.Angle(Vector3.right, camRoot.right);
|
|
|
+ angleY = Vector3.Angle(Vector3.up, camRoot.up);
|
|
|
character.enabled = true;
|
|
|
+ head = transform.Find("Head");
|
|
|
+ m_camera = camRoot.transform.Find("Main Camera");
|
|
|
+ camInitPos = m_camera.localPosition;
|
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
@@ -54,7 +63,7 @@ public class MoveController : MonoBehaviour
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 角色移动旋转总逻辑
|
|
|
+ /// 锟斤拷色锟狡讹拷锟斤拷转锟斤拷锟竭硷拷
|
|
|
/// </summary>
|
|
|
void CharacterMovement()
|
|
|
{
|
|
@@ -62,7 +71,7 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
jumpTime += Time.deltaTime;
|
|
|
}
|
|
|
- // 检测是否在地面上
|
|
|
+ // 锟斤拷锟斤拷欠锟斤拷诘锟斤拷锟斤拷锟�
|
|
|
//isGrounded = IsGrounded();
|
|
|
isGrounded = IsGrounded();
|
|
|
if (isGrounded && state == PlayerState.JumpDown)
|
|
@@ -74,7 +83,7 @@ public class MoveController : MonoBehaviour
|
|
|
//Debug.Log(isGrounded);
|
|
|
if (state == PlayerState.Ground)
|
|
|
{
|
|
|
- // 按下空格键触发跳跃
|
|
|
+ // 锟斤拷锟铰空革拷锟斤拷锟斤拷锟斤拷锟皆�
|
|
|
if (playerInput.JumpInput)
|
|
|
{
|
|
|
//state = PlayerState.JumpUp;
|
|
@@ -101,7 +110,7 @@ public class MoveController : MonoBehaviour
|
|
|
move = Vector3.zero;
|
|
|
if (state != PlayerState.Ground)
|
|
|
{
|
|
|
- // 应用重力
|
|
|
+ // 应锟斤拷锟斤拷锟斤拷
|
|
|
ApplyGravity();
|
|
|
}
|
|
|
else
|
|
@@ -113,25 +122,26 @@ public class MoveController : MonoBehaviour
|
|
|
* Time.deltaTime
|
|
|
* speed;
|
|
|
|
|
|
- // 移动物体
|
|
|
+ // 锟狡讹拷锟斤拷锟斤拷
|
|
|
Move();
|
|
|
+ CameraLimitation();
|
|
|
UpdateControlRotation();
|
|
|
}
|
|
|
|
|
|
void Jump()
|
|
|
{
|
|
|
- // 计算跳跃速度以达到指定的跳跃高度
|
|
|
+ // 锟斤拷锟斤拷锟斤拷跃锟劫讹拷锟皆达到指锟斤拷锟斤拷锟斤拷跃锟竭讹拷
|
|
|
verticalVelocity = Mathf.Sqrt(-2.0f * gravity * jumpHeight);
|
|
|
}
|
|
|
|
|
|
void ApplyGravity()
|
|
|
{
|
|
|
- // 应用重力以模拟物体的垂直运动
|
|
|
+ // 应锟斤拷锟斤拷锟斤拷锟斤拷模锟斤拷锟斤拷锟斤拷拇锟街憋拷硕锟�
|
|
|
verticalVelocity += gravity * Time.deltaTime;
|
|
|
- // 根据垂直速度移动物体
|
|
|
+ // 锟斤拷锟捷达拷直锟劫讹拷锟狡讹拷锟斤拷锟斤拷
|
|
|
if (verticalVelocity < 0.1f && verticalVelocity > -0.1f && state == PlayerState.JumpUp)
|
|
|
{
|
|
|
- //Debug.Log("*************");
|
|
|
+ Debug.Log("*************");
|
|
|
state = PlayerState.JumpDown;
|
|
|
}
|
|
|
|
|
@@ -141,10 +151,6 @@ public class MoveController : MonoBehaviour
|
|
|
|
|
|
void Move()
|
|
|
{
|
|
|
- //Debug.Log(playerInput.name + "****" + move);
|
|
|
- //var camRot = new Vector3(0, camera.eulerAngles.y, 0);
|
|
|
-
|
|
|
-
|
|
|
if (playerInput.MoveInput != Vector2.zero)
|
|
|
{
|
|
|
Vector3 dir = Vector3.zero;
|
|
@@ -152,19 +158,19 @@ public class MoveController : MonoBehaviour
|
|
|
//Debug.Log(playerInput.MoveInput);
|
|
|
if (playerInput.MoveInput.x > 0)
|
|
|
{
|
|
|
- dir = Vector3.ProjectOnPlane(camera.right, Vector3.up);
|
|
|
+ dir = Vector3.ProjectOnPlane(camRoot.right, Vector3.up);
|
|
|
}
|
|
|
if (playerInput.MoveInput.x < 0)
|
|
|
{
|
|
|
- dir += Vector3.ProjectOnPlane(-camera.right, Vector3.up);
|
|
|
+ dir += Vector3.ProjectOnPlane(-camRoot.right, Vector3.up);
|
|
|
}
|
|
|
if (playerInput.MoveInput.y > 0)
|
|
|
{
|
|
|
- dir += Vector3.ProjectOnPlane(camera.forward, Vector3.up);
|
|
|
+ dir += Vector3.ProjectOnPlane(camRoot.forward, Vector3.up);
|
|
|
}
|
|
|
if (playerInput.MoveInput.y < 0)
|
|
|
{
|
|
|
- dir += Vector3.ProjectOnPlane(-camera.forward, Vector3.up);
|
|
|
+ dir += Vector3.ProjectOnPlane(-camRoot.forward, Vector3.up);
|
|
|
}
|
|
|
//Debug.Log(dir);
|
|
|
Vector3 targetDir = Vector3.RotateTowards(
|
|
@@ -187,8 +193,8 @@ public class MoveController : MonoBehaviour
|
|
|
//Debug.Log(move + "*******" + "************" + m);
|
|
|
var angle = AngleSigned(
|
|
|
Vector3.forward,
|
|
|
- Vector3.ProjectOnPlane(camera.forward, Vector3.up),
|
|
|
- transform.up
|
|
|
+ Vector3.ProjectOnPlane(camRoot.forward, Vector3.up),
|
|
|
+ transform.up
|
|
|
);
|
|
|
Quaternion rot = Quaternion.Euler(0, angle, 0);
|
|
|
//Vector3 move2 = camera.transform.TransformDirection(move);
|
|
@@ -213,7 +219,7 @@ public class MoveController : MonoBehaviour
|
|
|
character.Move(rm + gravityMove);
|
|
|
lastMove = rm;
|
|
|
controller.speed = rm.magnitude * 300;
|
|
|
- camera.position = character.transform.position + new Vector3(0, 1.148f, 0);
|
|
|
+ camRoot.position = character.transform.position + new Vector3(0, 1.148f, 0);
|
|
|
}
|
|
|
|
|
|
public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
|
|
@@ -229,8 +235,8 @@ public class MoveController : MonoBehaviour
|
|
|
|
|
|
bool IsGrounded()
|
|
|
{
|
|
|
- // 在这里你可以添加检测物体是否在地面上的逻辑
|
|
|
- // 例如射线检测、碰撞器检测等
|
|
|
+ // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接硷拷锟斤拷锟斤拷锟斤拷欠锟斤拷诘锟斤拷锟斤拷系锟斤拷呒锟�
|
|
|
+ // 锟斤拷锟斤拷锟斤拷锟竭硷拷狻�拷锟阶诧拷锟斤拷锟斤拷锟�
|
|
|
var pos = transform.position;
|
|
|
radius = character.radius * 0.9f;
|
|
|
pointBottom = pos + transform.up * radius - transform.up * 0.1f;
|
|
@@ -259,14 +265,11 @@ public class MoveController : MonoBehaviour
|
|
|
//camera.eulerAngles += (new Vector3(playerInput.touch0Input.y, playerInput.touch0Input.x, 0)) * 0.1f;
|
|
|
angleX += playerInput.touch0Input.x * 10 * Time.fixedDeltaTime;
|
|
|
angleY -= playerInput.touch0Input.y * 10 * Time.fixedDeltaTime;
|
|
|
- angleY = angleClamp(angleY, -1, 60); //相机旋转角度限制
|
|
|
+ angleY = angleClamp(angleY, -45, 60);//锟斤拷锟斤拷锟阶�拷嵌锟斤拷锟斤拷锟�
|
|
|
+
|
|
|
+ //使锟矫诧拷值平锟斤拷锟斤拷转
|
|
|
+ camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
|
|
|
|
|
|
- //使用插值平滑旋转
|
|
|
- camera.rotation = Quaternion.Lerp(
|
|
|
- camera.rotation,
|
|
|
- Quaternion.Euler(angleY, angleX, 0),
|
|
|
- Time.fixedDeltaTime * 10
|
|
|
- );
|
|
|
}
|
|
|
if (
|
|
|
playerInput.touch1Input != Vector2.zero
|
|
@@ -275,30 +278,22 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
|
|
|
angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
|
|
|
- angleY = angleClamp(angleY, -1, 60); //相机旋转角度限制
|
|
|
+ angleY = angleClamp(angleY, -45, 60);//锟斤拷锟斤拷锟阶�拷嵌锟斤拷锟斤拷锟�
|
|
|
|
|
|
- //使用插值平滑旋转
|
|
|
- camera.rotation = Quaternion.Lerp(
|
|
|
- camera.rotation,
|
|
|
- Quaternion.Euler(angleY, angleX, 0),
|
|
|
- Time.fixedDeltaTime * 10
|
|
|
- );
|
|
|
+ //使锟矫诧拷值平锟斤拷锟斤拷转
|
|
|
+ camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
|
|
|
}
|
|
|
|
|
|
angleX += playerInput.CameraInput.x * 10 * Time.fixedDeltaTime;
|
|
|
angleY -= playerInput.CameraInput.y * 10 * Time.fixedDeltaTime;
|
|
|
- angleY = angleClamp(angleY, -1, 60); //相机旋转角度限制
|
|
|
+ angleY = angleClamp(angleY, -45, 60);//锟斤拷锟斤拷锟阶�拷嵌锟斤拷锟斤拷锟�
|
|
|
|
|
|
- //使用插值平滑旋转
|
|
|
- camera.rotation = Quaternion.Lerp(
|
|
|
- camera.rotation,
|
|
|
- Quaternion.Euler(angleY, angleX, 0),
|
|
|
- Time.fixedDeltaTime * 10
|
|
|
- );
|
|
|
+ //使锟矫诧拷值平锟斤拷锟斤拷转
|
|
|
+ camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 10);
|
|
|
}
|
|
|
|
|
|
private static float angleClamp(float angle, float min, float max)
|
|
|
- { //一个限制角度最大最小值的方法
|
|
|
+ { //一锟斤拷锟斤拷锟狡角讹拷锟斤拷锟斤拷锟叫≈碉拷姆锟斤拷锟�
|
|
|
if (angle < -360)
|
|
|
angle += 360;
|
|
|
if (angle > 360)
|
|
@@ -306,9 +301,78 @@ public class MoveController : MonoBehaviour
|
|
|
return Mathf.Clamp(angle, min, max);
|
|
|
}
|
|
|
|
|
|
- //void OnControllerColliderHit(ControllerColliderHit hit)
|
|
|
- //{
|
|
|
- // Debug.Log(hit.moveDirection + "************");
|
|
|
- // colliderDir = hit.moveDirection;
|
|
|
- //}
|
|
|
+ /// <summary>
|
|
|
+ /// 锟斤拷锟斤拷锟剿称撅拷头锟斤拷锟斤拷锟狡讹拷
|
|
|
+ /// </summary>
|
|
|
+ private void CameraLimitation()
|
|
|
+ {
|
|
|
+ float cosAngle = Vector3.Dot(m_camera.forward, Vector3.up);
|
|
|
+ float angleRad = Mathf.Asin(cosAngle);
|
|
|
+ float angleDeg = Mathf.Rad2Deg * angleRad;
|
|
|
+ float sign = Mathf.Sign(Vector3.Dot(Vector3.Cross(m_camera.forward, Vector3.up), Vector3.up));
|
|
|
+ float finalAngle = sign * angleDeg;
|
|
|
+ //float moveDistance = Mathf.Sin(Mathf.Deg2Rad * Mathf.Clamp(finalAngle, 0f, 10)) * 2;
|
|
|
+ float z, obsDistance=0,camDistance=0;
|
|
|
+ //Debug.Log(finalAngle);
|
|
|
+
|
|
|
+ //锟斤拷锟竭硷拷锟斤拷锟斤拷
|
|
|
+ //Ray ray = new Ray(head.position, m_camera.position - head.position);
|
|
|
+ //RaycastHit temphit;
|
|
|
+ //if (Physics.Raycast(ray, out temphit, 10f))
|
|
|
+ //{
|
|
|
+ // if (temphit.collider.name != "Main Camera")
|
|
|
+ // {
|
|
|
+ // isBlock = true;
|
|
|
+ // hit = temphit;
|
|
|
+ // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.1f);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // Debug.Log(temphit.collider.name);
|
|
|
+
|
|
|
+ //}
|
|
|
+
|
|
|
+ //if (isBlock)
|
|
|
+ //{
|
|
|
+ // ////锟斤拷锟斤拷锟斤拷转锟斤拷头锟斤拷锟斤拷
|
|
|
+ // //if (finalAngle > 1)
|
|
|
+ // //{
|
|
|
+ // // z = finalAngle * 0.1f;
|
|
|
+ // // camDistance = Vector3.Distance(camInitPos + new Vector3(0, 0, z),head.position);
|
|
|
+ // // Debug.DrawLine(temphit.point, head.position, Color.red);
|
|
|
+ // // Debug.DrawLine(camInitPos + new Vector3(0, 0, z), head.position, Color.blue);
|
|
|
+ // // Debug.Log(hit.collider.name+"))"+obsDistance + " // " + camDistance);
|
|
|
+ // // //if (obsDistance < camDistance && obsDistance > 0)
|
|
|
+ // // //{
|
|
|
+ // // // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.1f);
|
|
|
+ // // //}
|
|
|
+ // // //else
|
|
|
+ // // //{
|
|
|
+ // // // m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
|
|
|
+ // // //}
|
|
|
+
|
|
|
+ // //}
|
|
|
+ // //else
|
|
|
+ // {
|
|
|
+ // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //else
|
|
|
+ //{
|
|
|
+ //锟斤拷锟斤拷锟斤拷转锟斤拷头锟斤拷锟斤拷
|
|
|
+ if (finalAngle > 1)
|
|
|
+ {
|
|
|
+ z = finalAngle * 0.1f;
|
|
|
+ m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
|
|
|
+ }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+
|
|
|
}
|