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

131 lines
3.9 KiB
C#
Raw Normal View History

2024-11-12 10:42:00 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
2024-11-12 10:42:00 +08:00
public class PlayerMovement_Jpystick : MonoBehaviour, IDragHandler
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
2024-11-19 16:30:22 +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;
public CameraControl cameraControl;
2024-11-12 16:58:29 +08:00
private void Start()
{
characterControl = GetComponent<CharacterControl>();
Characterain = GetComponent<CharacterAin>();
rb = GetComponent<Rigidbody>();
}
public void OnDrag(PointerEventData eventData)
2024-11-12 10:42:00 +08:00
{
Debug.Log("=-=-----");
2024-11-12 20:07:19 +08:00
}
public void HandleJoystickControl(float horizontal, float vertical)
2024-11-12 20:07:19 +08:00
{
2024-11-19 11:45:07 +08:00
2024-11-12 10:42:00 +08:00
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>)
//Vector3 inputDirection = new Vector3(horizontal, 0f, vertical);
2024-11-12 16:58:29 +08:00
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>ɫ
Vector3 newPosition = rb.position + moveDirection * moveSpeed * Time.deltaTime;
2024-11-12 20:07:19 +08:00
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
}
public void HandleViewSwipe()
{
cameraControl.CamerMove();
2024-11-19 17:02:55 +08:00
// // <20><><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD>߼<EFBFBD>
// 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 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
}