Files
3d-fengji/Assets/Scripts/juese_chuangke260320/ControlMovingAndAnimation.cs
2026-05-20 17:05:47 +08:00

148 lines
4.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
//using static UnityEditor.Searcher.SearcherWindow.Alignment;
public class ControlMovingAndAnimation : MonoBehaviour
{
public Camera workerCamera;
public GameObject bear;
//Animation part
private Animator anim;
private CharacterController chara;
private NavMeshAgent agent;
//control moving part
float Gravity = -9.8f;
float yVelocity = 0f;
private float MoveSpeed = 4f;
// cinamachine part
public CinemachineFreeLook freeLook;
public Transform head;
private Vector3 lastPosition;
//hit part
// private Vector3 lastPos;
//public Transform hitPoint;
// public float radius = 0.3f;
// private HashSet<Collider> hitTargets = new HashSet<Collider>();
// public bool isAttacking = false;
void Start()
{
// lastPosition = transform.position;
anim = GetComponent<Animator>();
// agent = GetComponent<NavMeshAgent>();
chara = GetComponent<CharacterController>();
//lastPos = hitPoint.position;
}
// Update is called once per frame
void Update()
{
//这些是点哪走哪
//Vector3 currentPosition = transform.position;
//if (currentPosition != lastPosition)
//{
// anim.SetBool("IsWalking", true);
//}
//else
//{
// anim.SetBool("IsWalking", false);
//}
//lastPosition = currentPosition;
//if (Input.GetMouseButtonDown(0))
//{
// Ray r = workerCamera.ScreenPointToRay(Input.mousePosition);
// if (Physics.Raycast(r, out RaycastHit hit))
// {
// agent.SetDestination(hit.point);
// }
//}
//这些是键盘逻辑
FreeFalling();
PlayAnimations();
Move();
}
private void FreeFalling()
{
if (chara.isGrounded && yVelocity < 0f)
yVelocity = -2f;
else
yVelocity += Gravity * Time.deltaTime;
chara.Move(new Vector3(0f, yVelocity, 0f) * Time.deltaTime);
}
private void PlayAnimations()
{
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
anim.SetBool("IsWalking", true);
}
else
{
anim.SetBool("IsWalking", false);
}
if (Input.GetKey(KeyCode.LeftShift))
{
MoveSpeed = 8f;
anim.SetBool("IsRunning", true);
}
else
{
MoveSpeed = 4f;
anim.SetBool("IsRunning", false);
}
if (Input.GetKeyDown(KeyCode.Space) && chara.isGrounded)
{
anim.SetTrigger("Jump");
yVelocity = Mathf.Sqrt(20f);//向上初速度为根号10米每秒大概跳0.5米
}
//if (Input.GetMouseButtonDown(0))
//{
// anim.SetTrigger("Fight");
// // StartAttacking();
//}
}
private void Move()
{
Vector3 cameraWorldPos = freeLook.transform.position;//虚拟相机的位置
Vector3 cameraToPlayerHeadVector = (head.position - cameraWorldPos).normalized;//相机指向角色的头,这个代表指向方向向量
Vector3 horizontalDirection = new Vector3(cameraToPlayerHeadVector.x, 0, cameraToPlayerHeadVector.z).normalized;//指向向量去除y方向再归一化
Vector3 cameraRight = Vector3.Cross(Vector3.up, horizontalDirection).normalized;//垂直于相机指向,右手边
float DeltaHorizontal = Input.GetAxis("Horizontal");
float DeltaVertical = Input.GetAxis("Vertical");//两个方向位移检测 -1到1
Vector3 forwardMove = horizontalDirection * DeltaVertical;//视线指向方向乘上你按键的量,决定前进还是后退 W必然前进s必然朝你走
//Vector3 rightMove = cameraRight * DeltaHorizontal;
//Vector3 move = forwardMove + rightMove;
//Vector3 realMove = move.normalized * MoveSpeed * Time.deltaTime;
Vector3 realMove = forwardMove * MoveSpeed * Time.deltaTime;
chara.Move(realMove);
if (forwardMove.sqrMagnitude > 0f)
{
Quaternion targetRotation = Quaternion.LookRotation(forwardMove);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 5f);
}
}
//public void StartAttacking()
//{
// isAttacking = true;
// hitTargets.Clear();
//}
}