fix:代码提初始化

This commit is contained in:
zhangjiajia
2026-05-08 15:34:53 +08:00
parent af67dcce8c
commit 2540141343
4131 changed files with 1239331 additions and 0 deletions

View File

@@ -0,0 +1,145 @@
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>();
// chara = GetComponent<CharacterController>();
agent = GetComponent<NavMeshAgent>();
//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;
// FreeFalling();
// PlayAnimations();
// Move();
if (Input.GetMouseButtonDown(0))
{
Ray r = workerCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(r,out RaycastHit hit))
{
agent.SetDestination(hit.point);
}
}
}
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();
//}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58bf345e57e03a24fa0fd3bd4905190d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: