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

250 lines
6.5 KiB
C#
Raw Normal View History

2024-10-21 10:08:49 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-10-21 18:09:07 +08:00
using UnityEngine.UI;
2024-10-21 10:08:49 +08:00
public class CharacterControl : MonoBehaviour
{
// <20><>ǰ<EFBFBD>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
private float currentSpeed = 0f;
private float speedSmoothVelocity = 0f; // <20><><EFBFBD>ڴ洢<DAB4>ٶ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>ɵIJο<C4B2>ֵ
//<2F>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ٶ<EFBFBD>
public float moveSpeed=1f;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>ƶ<EFBFBD><C6B6>ķ<EFBFBD><C4B7><EFBFBD>
public Camera mainCamera;
private Vector3 moveDirection = Vector3.zero;
//<2F><>ɫrigidbody
private Rigidbody body;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public float JumpAbility = 1f;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
public float JumpMoveSpeed = 1f;
//<2F><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private CharacterAin Characterain;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float vertical;
float horizontal;
//<2F><>ֱ<EFBFBD><D6B1>ˮƽ<CBAE><C6BD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Vector3 dir;
2024-11-12 10:42:00 +08:00
2024-10-21 10:08:49 +08:00
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ƶ<EFBFBD>
private bool IsMoving = false;
2024-10-23 20:48:23 +08:00
2024-10-21 10:08:49 +08:00
// <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
public float fovChangeSpeed = 2f; // FOV<4F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
//<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-10-24 12:46:20 +08:00
2024-10-23 11:31:35 +08:00
2024-10-23 20:48:23 +08:00
//private Animator animator;
//private AnimatorStateInfo stateInfo;
private void Awake()
{
Init();
}
2024-10-23 11:31:35 +08:00
2024-10-21 10:08:49 +08:00
// Start is called before the first frame update
void Start()
{
2024-11-12 10:42:00 +08:00
2024-11-12 16:58:29 +08:00
2024-10-21 10:08:49 +08:00
}
// Update is called once per frame
void Update()
{
2024-10-23 20:48:23 +08:00
// Jump();
2024-10-21 10:08:49 +08:00
//JumpMove();
2024-10-23 20:48:23 +08:00
2024-10-21 10:08:49 +08:00
}
private void FixedUpdate()
{
Move();
Face();
}
//<2F><>ʼ<EFBFBD><CABC>
void Init()
{
body = transform.GetComponent<Rigidbody>();
if (body==null)
{
body = gameObject.AddComponent<Rigidbody>();
}
Characterain = transform.GetComponent<CharacterAin>();
2024-10-23 20:48:23 +08:00
if (Characterain==null)
{
Debug.LogError("Characterain==null");
}
2024-10-21 10:08:49 +08:00
}
2024-11-18 17:19:35 +08:00
2024-10-21 10:08:49 +08:00
//<2F>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ߡ<EFBFBD><DFA1><EFBFBD><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>or<6F>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>
void Move()
{
2024-10-23 20:48:23 +08:00
2024-10-21 10:08:49 +08:00
//<2F><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><E6B2BB><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Characterain.characterState==CharacterState.jump<6D><70><EFBFBD><EFBFBD><EFBFBD>ܻᵼ<DCBB><E1B5BC><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD><E4B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD>䣬˲<E4A3AC><CBB2>תΪ<D7AA>ƶ<EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD>޷<EFBFBD><DEB7>ָ<EFBFBD>վ<EFBFBD><D5BE>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>charaterAin<69>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>update<74><EFBFBD><EFA1A3><EFBFBD><EFBFBD>д<EFBFBD><D0B4>fixedupdate<74><65>
2024-10-23 20:48:23 +08:00
if (!Characterain.IsOnGround() || Characterain.characterState == CharacterState.jump)
2024-10-21 10:08:49 +08:00
{
return;
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");
2024-11-12 16:58:29 +08:00
//vertical = PlayerMovement_Jpystick.joystick.Vertical;
//horizontal = PlayerMovement_Jpystick.joystick.Horizontal;
2024-10-21 10:08:49 +08:00
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
Vector3 forward = mainCamera.transform.forward; // <20><><EFBFBD><EFBFBD>ͷǰ<CDB7><C7B0>
Vector3 right = mainCamera.transform.right; // <20><><EFBFBD><EFBFBD>ͷ<EFBFBD>ҷ<EFBFBD>
// <20><> Y <20><><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
forward.y = 0f;
right.y = 0f;
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
forward.Normalize();
right.Normalize();
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
dir = forward * vertical + right * horizontal;
//dir = transform.forward * vertical +transform.right * horizontal;
//<2F><><EFBFBD><EFBFBD>
//<2F>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>ʱֻҪ<D6BB><D2AA>ǰ<EFBFBD>ƶ<EFBFBD><C6B6>Ķ<EFBFBD><C4B6><EFBFBD>
if (Mathf.Abs(vertical) > 0.01f || Mathf.Abs(horizontal) > 0.01f)
{
if (MoveTime< (walkTime + runTime+0.1f))
{
MoveTime += Time.deltaTime;
}
MoveState();
if (!IsMoving)
{
IsMoving = true;
}
}
else if(Characterain.GetCharacterState()!= CharacterState.jump)
{
Characterain.SetPlayerState(CharacterState.idle);
IsMoving = false;
MoveTime = 0f;
}
}
//<2F>ƶ<EFBFBD>״̬
2024-11-12 16:58:29 +08:00
public void MoveState()
2024-10-21 10:08:49 +08:00
{
if (MoveTime < walkTime) // <20><><EFBFBD><EFBFBD>
{
Characterain.SetPlayerState(CharacterState.SlowWalk);
body.MovePosition(transform.position + dir * Time.deltaTime * moveSpeed * 1f);
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);
body.MovePosition(transform.position + dir * Time.deltaTime * moveSpeed * 1.5f);
targetFOV = normalFOV + 30f; // <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>FOVΪ<56><CEAA><EFBFBD><EFBFBD>ֵ
}
else // <20><><EFBFBD><EFBFBD>
{
Characterain.SetPlayerState(CharacterState.run);
body.MovePosition(transform.position + dir * Time.deltaTime * moveSpeed * 2f);
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);
}
//<2F><><EFBFBD><EFBFBD>
void Face()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB7BD>
Vector3 moveInput = new Vector3(horizontal, 0f, vertical).normalized;
if (moveInput.magnitude > 0.1f) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫӦ<C9AB><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E7B7BD>
Vector3 targetDirection = mainCamera.transform.forward * vertical + mainCamera.transform.right * horizontal;
targetDirection.y = 0f; // <20><><EFBFBD><EFBFBD>Y<EFBFBD><59><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ֻ<EFBFBD><D6BB>עˮƽ<CBAE><C6BD>ת
targetDirection.Normalize();
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ת<EFBFBD>Ƕ<EFBFBD>
Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
// ʹ<><CAB9>Slerpƽ<70><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ŀ<EFBFBD><C4BF><EFBFBD>Ƕȣ<C7B6>rotationSpeed<65><64><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
float rotationSpeed = 10f; // <20><>ת<EFBFBD>ٶȿɵ<C8BF>
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
2024-10-21 18:09:07 +08:00
//jump<6D>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>
2024-10-21 10:08:49 +08:00
//<2F><>
2024-10-21 18:09:07 +08:00
//void Jump()
//{
// if (Input.GetKey(KeyCode.Space) && Characterain.GetCharacterState() != CharacterState.jump)
// {
// body.AddForce(Vector3.up * JumpAbility * 2F, ForceMode.Impulse);
// Characterain.SetPlayerState(CharacterState.jump);
// }
//}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
//void JumpMove()
//{
// if (IsMoving && !Characterain.IsOnGround())
// {
// Vector3 airMoveForce = dir.normalized * JumpMoveSpeed * 0.5f;
// body.AddForce(airMoveForce, ForceMode.Force);
// }
//}
2024-10-23 20:48:23 +08:00
public void Restore()//<2F><><EFBFBD><EFBFBD>fire<72>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD>ɵĽű<C4BD><C5B1><EFBFBD><EFBFBD><EFBFBD>
{
this.enabled = true;
}
2024-10-22 11:57:50 +08:00
/* public NpcState Setnpcstate()
{
Debug.Log("Setnpcstate<74><65><EFBFBD><EFBFBD>");
return NpcState.Healthy;
}*/
2024-10-21 10:08:49 +08:00
}