|
@@ -1,4 +1,3 @@
|
|
|
-using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
@@ -43,14 +42,14 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
controller = GetComponent<AnimatorController>();
|
|
|
character = GetComponent<CharacterController>();
|
|
|
- playerInput=GetComponent<PlayerInputComponent>();
|
|
|
+ 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;
|
|
|
- oriDistance = Vector3.Distance(head.position, m_camera.position-m_camera.forward*2);
|
|
|
+ oriDistance = Vector3.Distance(head.position, m_camera.position - m_camera.forward * 2);
|
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
@@ -72,7 +71,7 @@ public class MoveController : MonoBehaviour
|
|
|
}
|
|
|
// ����Ƿ��ڵ�����
|
|
|
//isGrounded = IsGrounded();
|
|
|
- isGrounded =character.isGrounded|| IsGrounded();
|
|
|
+ isGrounded = character.isGrounded || IsGrounded();
|
|
|
if (isGrounded && state == PlayerState.JumpDown)
|
|
|
{
|
|
|
verticalVelocity = 0;
|
|
@@ -120,8 +119,8 @@ public class MoveController : MonoBehaviour
|
|
|
new Vector3(playerInput.MoveInput.x, 0, playerInput.MoveInput.y)
|
|
|
* Time.deltaTime
|
|
|
* speed;
|
|
|
-
|
|
|
// �ƶ�����
|
|
|
+
|
|
|
Move();
|
|
|
UpdateControlRotation();
|
|
|
}
|
|
@@ -155,23 +154,35 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
Vector3 dir = Vector3.zero;
|
|
|
|
|
|
- //Debug.Log(playerInput.MoveInput);
|
|
|
if (playerInput.MoveInput.x > 0)
|
|
|
{
|
|
|
- dir = Vector3.ProjectOnPlane(camRoot.right, Vector3.up);
|
|
|
+ dir = Vector3.ProjectOnPlane(
|
|
|
+ camRoot.right * Mathf.Abs(playerInput.MoveInput.x),
|
|
|
+ Vector3.up
|
|
|
+ );
|
|
|
}
|
|
|
if (playerInput.MoveInput.x < 0)
|
|
|
{
|
|
|
- dir += Vector3.ProjectOnPlane(-camRoot.right, Vector3.up);
|
|
|
+ dir += Vector3.ProjectOnPlane(
|
|
|
+ -camRoot.right * Mathf.Abs(playerInput.MoveInput.x),
|
|
|
+ Vector3.up
|
|
|
+ );
|
|
|
}
|
|
|
if (playerInput.MoveInput.y > 0)
|
|
|
{
|
|
|
- dir += Vector3.ProjectOnPlane(camRoot.forward, Vector3.up);
|
|
|
+ dir += Vector3.ProjectOnPlane(
|
|
|
+ camRoot.forward * Mathf.Abs(playerInput.MoveInput.y),
|
|
|
+ Vector3.up
|
|
|
+ );
|
|
|
}
|
|
|
if (playerInput.MoveInput.y < 0)
|
|
|
{
|
|
|
- dir += Vector3.ProjectOnPlane(-camRoot.forward, Vector3.up);
|
|
|
+ dir += Vector3.ProjectOnPlane(
|
|
|
+ -camRoot.forward * Mathf.Abs(playerInput.MoveInput.y),
|
|
|
+ Vector3.up
|
|
|
+ );
|
|
|
}
|
|
|
+
|
|
|
//Debug.Log(dir);
|
|
|
Vector3 targetDir = Vector3.RotateTowards(
|
|
|
transform.forward,
|
|
@@ -185,24 +196,13 @@ public class MoveController : MonoBehaviour
|
|
|
Quaternion.LookRotation(targetDir),
|
|
|
Time.deltaTime * 15f
|
|
|
);
|
|
|
- //var a = Vector3.Angle(transform.forward.normalized, playerInput.MoveInput.normalized);
|
|
|
- //Quaternion r = Quaternion.Euler(0, a, 0);
|
|
|
- //transform.eulerAngles = r * transform.eulerAngles;
|
|
|
}
|
|
|
- //var m = move.magnitude * camera.forward + gravityMove;
|
|
|
- //Debug.Log(move + "*******" + "************" + m);
|
|
|
var angle = AngleSigned(
|
|
|
Vector3.forward,
|
|
|
Vector3.ProjectOnPlane(camRoot.forward, Vector3.up),
|
|
|
- transform.up
|
|
|
+ transform.up
|
|
|
);
|
|
|
Quaternion rot = Quaternion.Euler(0, angle, 0);
|
|
|
- //Vector3 move2 = camera.transform.TransformDirection(move);
|
|
|
-
|
|
|
-
|
|
|
- //Debug.Log(move.magnitude + "***********" + gravityMove.magnitude);
|
|
|
-
|
|
|
- //var m = move * 0.1f + lastMove * 0.9f;
|
|
|
var rm = rot * move;
|
|
|
if (gravityMove != Vector3.zero)
|
|
|
{
|
|
@@ -211,14 +211,10 @@ public class MoveController : MonoBehaviour
|
|
|
|
|
|
rm = rm * 0.1f + lastMove * 0.9f;
|
|
|
|
|
|
- //if (state == PlayerState.JumpDown)
|
|
|
- //{
|
|
|
- // character.Move(-colliderDir * Time.deltaTime);
|
|
|
- //}
|
|
|
-
|
|
|
+ controller.speed = rm.magnitude * 300;
|
|
|
character.Move(rm + gravityMove);
|
|
|
lastMove = rm;
|
|
|
- controller.speed = rm.magnitude * 300;
|
|
|
+
|
|
|
camRoot.position = character.transform.position + new Vector3(0, 1.148f, 0);
|
|
|
}
|
|
|
|
|
@@ -265,11 +261,14 @@ 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, -45, 60);//�����ת�Ƕ�����
|
|
|
+ angleY = angleClamp(angleY, -45, 60); //�����ת�Ƕ�����
|
|
|
|
|
|
//ʹ�ò�ֵƽ����ת
|
|
|
- camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
|
|
|
-
|
|
|
+ camRoot.rotation = Quaternion.Lerp(
|
|
|
+ camRoot.rotation,
|
|
|
+ Quaternion.Euler(angleY, angleX, 0),
|
|
|
+ Time.fixedDeltaTime * 100
|
|
|
+ );
|
|
|
}
|
|
|
if (
|
|
|
playerInput.touch1Input != Vector2.zero
|
|
@@ -278,18 +277,26 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
|
|
|
angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
|
|
|
- angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
|
|
|
+ angleY = angleClamp(angleY, -45, 60); //�����ת�Ƕ�����
|
|
|
|
|
|
//ʹ�ò�ֵƽ����ת
|
|
|
- camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
|
|
|
+ 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, -45, 60);//�����ת�Ƕ�����
|
|
|
+ angleY = angleClamp(angleY, -45, 60); //�����ת�Ƕ�����
|
|
|
|
|
|
//ʹ�ò�ֵƽ����ת
|
|
|
- camRoot.rotation = Quaternion.Lerp(camRoot.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)
|
|
@@ -309,10 +316,14 @@ public class MoveController : MonoBehaviour
|
|
|
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 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;
|
|
|
+ float z,
|
|
|
+ obsDistance = 0,
|
|
|
+ camDistance = 0;
|
|
|
//Debug.Log(finalAngle);
|
|
|
|
|
|
//射线检测是否有障碍物
|
|
@@ -320,24 +331,17 @@ public class MoveController : MonoBehaviour
|
|
|
|
|
|
RaycastHit[] temphit;
|
|
|
RaycastHit hit;
|
|
|
- var cos = Physics.Raycast(ray, out hit, oriDistance, ~(1 << 6|1<<18));
|
|
|
+ var cos = Physics.Raycast(ray, out hit, oriDistance, ~(1 << 6 | 1 << 18));
|
|
|
if (cos)
|
|
|
{
|
|
|
-
|
|
|
Debug.Log(hit.collider.name);
|
|
|
m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
|
|
|
-
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
-
|
|
|
m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
//RaycastHit[] temphit;
|
|
|
//var cos = Physics.RaycastAll(head.position, m_camera.position - head.position, oriDistance, ~(1 << 6));
|
|
|
//if (cos.Length>0)
|
|
@@ -390,7 +394,7 @@ public class MoveController : MonoBehaviour
|
|
|
// Vector3 newPoint = head.position + p;
|
|
|
// m_camera.localPosition = Vector3.Lerp(m_camera.position, new Vector3(0,1.48f,-5), 0.5f);
|
|
|
//}
|
|
|
- //
|
|
|
+ //
|
|
|
|
|
|
//}
|
|
|
|
|
@@ -426,16 +430,19 @@ public class MoveController : MonoBehaviour
|
|
|
{
|
|
|
z = finalAngle * 0.1f;
|
|
|
var pos = camInitPos + new Vector3(0, 0, z);
|
|
|
- var dis1 =Vector3.Distance(head.position, pos);
|
|
|
- var dis2 =Vector3.Distance(m_camera.position, head.position);
|
|
|
- if(!cos)
|
|
|
+ var dis1 = Vector3.Distance(head.position, pos);
|
|
|
+ var dis2 = Vector3.Distance(m_camera.position, head.position);
|
|
|
+ if (!cos)
|
|
|
m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
if (m_camera.localPosition.z > -0.4f)
|
|
|
{
|
|
|
- m_camera.localPosition = new Vector3(m_camera.localPosition.x, m_camera.localPosition.y,-0.4f);
|
|
|
+ m_camera.localPosition = new Vector3(
|
|
|
+ m_camera.localPosition.x,
|
|
|
+ m_camera.localPosition.y,
|
|
|
+ -0.4f
|
|
|
+ );
|
|
|
}
|
|
|
//else
|
|
|
//{
|