相机移动bug

This commit is contained in:
杨号敬 2024-12-25 17:06:23 +08:00
parent 9f14a4e7d0
commit 8ae97dee20
6 changed files with 132 additions and 107 deletions

View File

@ -159,15 +159,15 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 7256832922758735078, guid: 2b6da6cb0c6edad4eb364a43fc9035cd, type: 3}
propertyPath: m_LocalPosition.x
value: -26.3
value: -27.32
objectReference: {fileID: 0}
- target: {fileID: 7256832922758735078, guid: 2b6da6cb0c6edad4eb364a43fc9035cd, type: 3}
propertyPath: m_LocalPosition.y
value: -2.04
value: -2.28
objectReference: {fileID: 0}
- target: {fileID: 7256832922758735078, guid: 2b6da6cb0c6edad4eb364a43fc9035cd, type: 3}
propertyPath: m_LocalPosition.z
value: -18.55
value: -17.27
objectReference: {fileID: 0}
- target: {fileID: 7256832922758735078, guid: 2b6da6cb0c6edad4eb364a43fc9035cd, type: 3}
propertyPath: m_LocalRotation.w
@ -14454,52 +14454,6 @@ Transform:
m_CorrespondingSourceObject: {fileID: 426926, guid: 363867a985f1ea248acb7ce0fb14b296, type: 3}
m_PrefabInstance: {fileID: 825899810}
m_PrefabAsset: {fileID: 0}
--- !u!1 &826696725
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 826696727}
- component: {fileID: 826696726}
m_Layer: 0
m_Name: BurstCollisionWorld
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &826696726
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 826696725}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3}
m_Name:
m_EditorClassIdentifier:
cellSpans:
m_AlignBytes: 16
--- !u!4 &826696727
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 826696725}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 38
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &827349627
PrefabInstance:
m_ObjectHideFlags: 0
@ -25112,6 +25066,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
yg: {fileID: 1221713538}
Player: {fileID: 382802809}
CameraControl: {fileID: 1068997308}
horizontal: 0
vertical: 0
--- !u!114 &1490008726
@ -29365,6 +29320,52 @@ Transform:
m_CorrespondingSourceObject: {fileID: 458710, guid: 7f0cbeb31df8d5a4e88fad0e1506c186, type: 3}
m_PrefabInstance: {fileID: 1788785149}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1790366129
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1790366131}
- component: {fileID: 1790366130}
m_Layer: 0
m_Name: BurstCollisionWorld
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1790366130
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1790366129}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3}
m_Name:
m_EditorClassIdentifier:
cellSpans:
m_AlignBytes: 16
--- !u!4 &1790366131
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1790366129}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 38
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1792217600
PrefabInstance:
m_ObjectHideFlags: 0
@ -46414,12 +46415,13 @@ MonoBehaviour:
joystickBackground: {fileID: 8163955216392696351}
joystick: {fileID: 8163955215854446702}
playerCamera: {fileID: 1068997306}
rotationSpeed: 0.5
rotationSpeed: 0.1
target: {fileID: 382802812}
horizontalMinRotation: -75
horizontalMaxRotation: 75
verticalMinRotation: -75
verticalMaxRotation: 75
joystickInput: {x: 0, y: 0}
--- !u!224 &8163955216411254552
RectTransform:
m_ObjectHideFlags: 0

View File

@ -39,6 +39,7 @@ public class CameraControl : MonoBehaviour
void Awake()
{
// 引用相机的变换组件。
cam = transform;
@ -63,66 +64,24 @@ public class CameraControl : MonoBehaviour
void Update()
{
// 获取鼠标移动来控制相机的旋转。
// 鼠标控制:
// 设置垂直移动的限制。
angleV = Mathf.Clamp(angleV, minVerticalAngle, targetMaxVerticalAngle);
// 设置垂直相机抖动(模拟后坐力)。
angleV = Mathf.LerpAngle(angleV, angleV + recoilAngle, 10f * Time.deltaTime);
//// 处理相机方向锁定。
if (firstDirection != Vector3.zero&& !joystickController.isDrag)
{
angleH -= deltaH;
UpdateLockAngle();
angleH += deltaH;
}
// 处理相机的水平旋转限制(如果设置了)。
if (forwardHorizontalRef != default(Vector3))
{
ClampHorizontal();
}
// 设置相机的方向。
Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, 0);
cam.rotation = aimRotation;
if (joystickController.joystickInput.magnitude > 0)
{
return;
}
cam.rotation = aimRotation;
// 设置相机的视场角。
cam.GetComponent<Camera>().fieldOfView = Mathf.Lerp(cam.GetComponent<Camera>().fieldOfView, targetFOV, Time.deltaTime);
// 基于当前相机位置,检测与环境的碰撞。
Vector3 baseTempPosition = Character.position + camYRotation * targetPivotOffset;
Vector3 noCollisionOffset = targetCamOffset;
while (noCollisionOffset.magnitude >= 0.2f)
{
if (DoubleViewingPosCheck(baseTempPosition + aimRotation * noCollisionOffset))
break;
noCollisionOffset -= noCollisionOffset.normalized * 0.2f;
}
if (noCollisionOffset.magnitude < 0.2f)
noCollisionOffset = Vector3.zero;
// 如果有自定义偏移,并且相机碰撞,将进入第一人称模式。
bool customOffsetCollision = isCustomOffset && noCollisionOffset.sqrMagnitude < targetCamOffset.sqrMagnitude;
// 重新定位相机。
smoothPivotOffset = Vector3.Lerp(smoothPivotOffset, customOffsetCollision ? pivotOffset : targetPivotOffset, smooth * Time.deltaTime);
smoothCamOffset = Vector3.Lerp(smoothCamOffset, customOffsetCollision ? Vector3.zero : noCollisionOffset, smooth * Time.deltaTime);
cam.position = Character.position + camYRotation * smoothPivotOffset + aimRotation * smoothCamOffset;
// 平滑相机的垂直抖动(后坐力)。
if (recoilAngle > 0)
recoilAngle -= 5 * Time.deltaTime;
else if (recoilAngle < 0)
recoilAngle += 5 * Time.deltaTime;
}
// 设置/取消水平旋转的限制。

View File

@ -7,6 +7,8 @@ public class DragTheScreen : MonoBehaviour, IDragHandler
{
public GameObject yg;
public GameObject Player;
public CameraControl CameraControl;
private bool isscanmove = true;
private void Start()
{
yg.GetComponent<Joystick>().Drag += DragEvent;
@ -14,7 +16,11 @@ public class DragTheScreen : MonoBehaviour, IDragHandler
}
public void OnDrag(PointerEventData eventData)//屏幕移动
{
if (isscanmove)
{
Player.GetComponent<PlayerMovement_Jpystick>().HandleViewSwipe();
}
}
public void DragEvent(float x, float y, PointerEventData eventData)//摇杆移动
{
@ -34,6 +40,16 @@ public class DragTheScreen : MonoBehaviour, IDragHandler
public float vertical = 0;
public void Update()
{
if (horizontal != 0 || vertical != 0) Player.GetComponent<PlayerMovement_Jpystick>().HandleJoystickControl(horizontal, vertical);
if (CameraControl.joystickController.joystickInput.magnitude > 0)
{
isscanmove = false;
if (horizontal != 0 || vertical != 0) Player.GetComponent<PlayerMovement_Jpystick>().HandleJoystickControl2(horizontal, vertical);
}
else
{
isscanmove = true;
if (horizontal != 0 || vertical != 0) Player.GetComponent<PlayerMovement_Jpystick>().HandleJoystickControl(horizontal, vertical);
}
}
}

View File

@ -95,7 +95,43 @@ public class PlayerMovement_Jpystick : MonoBehaviour, IDragHandler
}
public void HandleJoystickControl2(float horizontal, float vertical)
{
// 2. 转换为三维方向向量 (保持在水平面)
//Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
// 4. 计算最终的移动方向 (相对于摄像机的前后左右)
Vector3 moveDirection = this.transform.forward;
if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f)
{
if (MoveTime < (walkTime + runTime + 0.1f))
{
MoveTime += Time.deltaTime;
}
}
// 5. 应用移动
if (moveDirection.magnitude > 0.1f)
{
// 使用速度移动角色
Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.deltaTime;
rb.MovePosition(newPosition);
MoveState();
// 使角色面朝移动方向
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime);
}
}
public void HandleViewSwipe()
{
cameraControl.CamerMove();

View File

@ -212,6 +212,7 @@ public class Fire : MonoBehaviour
isPlayerInRange = false; // 鯤소잼역렀鍋
characterControl = null;
UseSkill= null;
us.FireOUt();
}
}

View File

@ -19,7 +19,7 @@ public class JoystickController : MonoBehaviour, IDragHandler, IEndDragHandler
public float verticalMinRotation = -75f; // 垂直旋转最小值
public float verticalMaxRotation = 75f; // 垂直旋转最大值
private Vector2 joystickInput = Vector2.zero;
public Vector2 joystickInput = Vector2.zero;
private float currentXRotation = 0f; // 当前的水平旋转角度
private float currentYRotation = 0f; // 当前的垂直旋转角度
@ -43,19 +43,29 @@ public class JoystickController : MonoBehaviour, IDragHandler, IEndDragHandler
if (joystickInput.magnitude > 0)
{
// 根据摇杆的输入控制摄像机旋转
targetXRotation += joystickInput.x * rotationSpeed * Time.deltaTime;
targetYRotation -= joystickInput.y * rotationSpeed * Time.deltaTime;
// 根据摇杆的输入控制摄像机旋转
targetXRotation = joystickInput.x * rotationSpeed * Time.deltaTime;
targetYRotation = joystickInput.y * rotationSpeed * Time.deltaTime;
Vector3 currentRotation = playerCamera.transform.eulerAngles;
Debug.Log(currentRotation);
float yRotation = currentRotation.y;
if (yRotation > 90f) targetYRotation = 0f;
if (yRotation < -90f) targetYRotation = 0f;
//// 限制水平旋转范围,防止摄像机旋转过头
//targetXRotation = Mathf.Clamp(targetXRotation, horizontalMinRotation, horizontalMaxRotation);
//// 限制垂直旋转范围,防止摄像机翻转
//targetYRotation = Mathf.Clamp(targetYRotation, verticalMinRotation, verticalMaxRotation);
playerCamera.transform.RotateAround(target.transform.position, playerCamera.transform.right, targetYRotation);
playerCamera.transform.RotateAround(target.transform.position, Vector3.up, targetXRotation);
targetXRotation = playerCamera.transform.eulerAngles.y;
targetYRotation = playerCamera.transform.eulerAngles.x;
playerCamera.transform.SetParent(target);
//playerCamera.transform.LookAt(new Vector3(target.transform.position.x,playerCamera.transform.position.y,target.transform.position.z));
// 使用 Quaternion 来平滑过渡到目标旋转角度
//playerCamera.transform.localRotation = Quaternion.Euler(targetYRotation, targetXRotation, 0f);
@ -63,6 +73,7 @@ public class JoystickController : MonoBehaviour, IDragHandler, IEndDragHandler
}
else
{
playerCamera.transform.SetParent(target.parent);
targetXRotation = 0;
targetYRotation = 0;
}