_xiaofang/xiaofang/Assets/Script/Character/PlayerMovement_Jpystick.cs

176 lines
5.4 KiB
C#
Raw Normal View History

2024-11-12 10:42:00 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-11-19 11:45:07 +08:00
public class PlayerMovement_Jpystick : MonoBehaviour
2024-11-12 10:42:00 +08:00
{
2024-11-12 16:58:29 +08:00
public FixedJoystick joystick; // <20><><EFBFBD><EFBFBD> Fixed Joystick
public Transform cameraTransform; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Transform
public float moveSpeed = 5f;
public CharacterControl characterControl;
private CharacterAin Characterain;
public Camera mainCamera;
private Rigidbody rb;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> FOV <20><><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>
public float normalFOV = 60f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>FOV
public float sprintFOV = 120f; // <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>FOV
2024-11-19 11:45:07 +08:00
public float fovChangeSpeed = 2f; // FOV<4F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
2024-11-19 10:30:41 +08:00
// <20><><EFBFBD><EFBFBD>ID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-19 11:45:07 +08:00
private int leftFingerId = -1;
private int rightFingerId = -1;
2024-11-12 20:07:19 +08:00
2024-11-12 16:58:29 +08:00
private bool IsMoving = false;
2024-11-19 11:45:07 +08:00
//<2F>ܲ<EFBFBD><DCB2>л<EFBFBD><D0BB><EFBFBD>ʱ<EFBFBD><CAB1>
private float MoveTime = 0f;
//<2F><>·<EFBFBD><C2B7>ʱ<EFBFBD><CAB1>
public float walkTime;
//<2F>ܵ<EFBFBD>ʱ<EFBFBD><CAB1>
public float runTime;
//<2F>ܲ<EFBFBD>ʱƽ<CAB1><C6BD><EFBFBD>л<EFBFBD>fov
private float targetFOV;
2024-11-12 16:58:29 +08:00
private void Start()
{
characterControl = GetComponent<CharacterControl>();
Characterain = GetComponent<CharacterAin>();
rb = GetComponent<Rigidbody>();
}
2024-11-19 11:45:07 +08:00
2024-11-12 10:42:00 +08:00
void Update()
{
2024-11-19 11:45:07 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>
2024-11-12 20:07:19 +08:00
foreach (Touch touch in Input.touches)
{
2024-11-19 11:45:07 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼʱ<CABC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4B4A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-12 20:07:19 +08:00
if (touch.phase == TouchPhase.Began)
{
if (touch.position.x < Screen.width / 2 && leftFingerId == -1)
{
2024-11-19 11:45:07 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬰<EFBFBD><F2A3ACB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָID<49><44><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҡ<EFBFBD><D2A1>
leftFingerId = touch.fingerId;
2024-11-12 20:07:19 +08:00
}
else if (touch.position.x >= Screen.width / 2 && rightFingerId == -1)
{
2024-11-19 11:45:07 +08:00
// <20>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>򣬰<EFBFBD><F2A3ACB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָID<49><44><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD>ӽ<EFBFBD>
rightFingerId = touch.fingerId;
2024-11-12 20:07:19 +08:00
}
}
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
if (touch.fingerId == leftFingerId)
{
2024-11-19 11:45:07 +08:00
// <20><><EFBFBD><EFBFBD><E0B4A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD>ƶ<EFBFBD>
HandleJoystickControl();
2024-11-12 20:07:19 +08:00
}
else if (touch.fingerId == rightFingerId)
{
2024-11-19 11:45:07 +08:00
// <20>Ҳഥ<D2B2><E0B4A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD>
2024-11-19 14:27:13 +08:00
//HandleViewSwipe(touch);
float horizontalSwipe = touch.deltaPosition.x * 0.1f; // <20>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float verticalSwipe = -touch.deltaPosition.y * 0.1f;
cameraTransform.Rotate(0, horizontalSwipe, 0, Space.World);
cameraTransform.Rotate(verticalSwipe, 0, 0, Space.Self);
2024-11-12 20:07:19 +08:00
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
2024-11-19 11:45:07 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID
2024-11-12 20:07:19 +08:00
if (touch.fingerId == leftFingerId)
{
2024-11-19 11:45:07 +08:00
leftFingerId = -1;
2024-11-12 20:07:19 +08:00
}
else if (touch.fingerId == rightFingerId)
{
2024-11-19 11:45:07 +08:00
rightFingerId = -1;
2024-11-12 20:07:19 +08:00
}
}
}
}
void HandleJoystickControl()
{
2024-11-19 11:45:07 +08:00
// 1. <20><>ȡҡ<C8A1><D2A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-12 10:42:00 +08:00
float horizontal = joystick.Horizontal;
float vertical = joystick.Vertical;
2024-11-19 11:45:07 +08:00
// 2. ת<><D7AA>Ϊ<EFBFBD><CEAA>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮƽ<CBAE><C6BD>)
2024-11-12 16:58:29 +08:00
Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
2024-11-19 11:45:07 +08:00
// 3. <20><>ȡ<EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E7B7BD>
2024-11-12 16:58:29 +08:00
Vector3 cameraForward = cameraTransform.forward; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-19 11:45:07 +08:00
Vector3 cameraRight = cameraTransform.right; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҷ<EFBFBD><D2B7><EFBFBD>
2024-11-12 16:58:29 +08:00
2024-11-19 11:45:07 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>Ҫˮƽ<CBAE><C6BD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>y<EFBFBD><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-12 16:58:29 +08:00
cameraForward.y = 0;
cameraRight.y = 0;
cameraForward.Normalize();
cameraRight.Normalize();
2024-11-19 11:45:07 +08:00
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
2024-11-12 16:58:29 +08:00
Vector3 moveDirection = (cameraRight * horizontal + cameraForward * vertical).normalized;
2024-11-19 11:45:07 +08:00
2024-11-12 16:58:29 +08:00
if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f)
{
if (MoveTime < (walkTime + runTime + 0.1f))
{
MoveTime += Time.deltaTime;
}
}
2024-11-19 11:45:07 +08:00
// 5. Ӧ<><D3A6><EFBFBD>ƶ<EFBFBD>
if (moveDirection.magnitude > 0.1f)
{
// ʹ<><CAB9><EFBFBD>ٶ<EFBFBD><D9B6>ƶ<EFBFBD><C6B6><EFBFBD>ɫ
2024-11-12 20:07:19 +08:00
Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime;
rb.MovePosition(newPosition);
MoveState();
// ʹ<><CAB9>ɫ<EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 720 * Time.deltaTime);
2024-11-19 11:45:07 +08:00
}
2024-11-12 20:07:19 +08:00
}
void HandleViewSwipe(Touch touch)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD>߼<EFBFBD>
2024-11-19 14:27:13 +08:00
2024-11-12 10:42:00 +08:00
}
2024-11-12 16:58:29 +08:00
public void MoveState()
{
if (MoveTime < walkTime) // <20><><EFBFBD><EFBFBD>
{
Characterain.SetPlayerState(CharacterState.SlowWalk);
targetFOV = normalFOV; // <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>FOVΪ<56><CEAA><EFBFBD><EFBFBD>ֵ
}
else if (MoveTime < (walkTime + runTime)) // <20><>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
{
Characterain.SetPlayerState(CharacterState.walk);
targetFOV = normalFOV + 30f; // <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>FOVΪ<56><CEAA><EFBFBD><EFBFBD>ֵ
}
else // <20><><EFBFBD><EFBFBD>
{
Characterain.SetPlayerState(CharacterState.run);
targetFOV = sprintFOV; // <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>FOVΪ<56><CEAA><EFBFBD><EFBFBD>ֵ
}
// ʹ<>ò<EFBFBD>ֵƽ<D6B5><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FOV
mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, targetFOV, fovChangeSpeed * Time.deltaTime);
}
2024-11-19 11:45:07 +08:00
}