初始化
This commit is contained in:
commit
f8ee849c7e
162 files changed
+14408
No files matched your search
@@ -0,0 +1,55 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AnimatedCharacterMovement : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed = 1f; // 移动速度
|
||||
private Animator animator;
|
||||
private CharacterController controller;
|
||||
|
||||
void Start()
|
||||
{
|
||||
animator = GetComponent<Animator>();
|
||||
controller = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
// 获取点击输入
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
// 获取点击位置
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hit;
|
||||
|
||||
// 检测点击位置是否有物体
|
||||
if (Physics.Raycast(ray, out hit))
|
||||
{
|
||||
// 获取点击位置的坐标
|
||||
Vector3 targetPosition = hit.point;
|
||||
|
||||
// 计算移动方向
|
||||
Vector3 movement = targetPosition - transform.position;
|
||||
movement.y = 0; // 确保只在水平面上旋转
|
||||
|
||||
//改变角色朝向
|
||||
transform.rotation = Quaternion.LookRotation(movement, Vector3.up);
|
||||
|
||||
//改变动画控制器的参数
|
||||
animator.SetInteger("speed", 1);
|
||||
|
||||
//角色像点击处移动
|
||||
//获取movement向量
|
||||
Vector3 move = movement.normalized;
|
||||
controller.Move(move * moveSpeed * Time.deltaTime);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
animator.SetInteger("speed", 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: CntKvHv4VXPNyZJDskcWyKYli31IutOA22jINaqbgxi9J3GgnvB9dMw=
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraFollow : MonoBehaviour
|
||||
{
|
||||
public Transform target; // 角色的Transform组件
|
||||
public Vector3 offset; // 摄像机相对于角色的偏移量
|
||||
public float smoothSpeed = 0.125f; // 摄像机移动的平滑速度
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
// // 计算摄像机应该到达的目标位置
|
||||
// Vector3 desiredPosition = target.position + offset;
|
||||
// // 使用Lerp方法平滑移动摄像机到目标位置
|
||||
// Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
|
||||
// transform.position = smoothedPosition;
|
||||
|
||||
// // 让摄像机始终看向角色
|
||||
// transform.LookAt(target);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: Bn0Ys3+tV3KnrXgFeVgvdVu6+Wt4AIUfkDqKj2dk/x6RqQPUii6D7+A=
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,161 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class NewBehaviourScript : MonoBehaviour
|
||||
{
|
||||
|
||||
//移动速度
|
||||
public float speed = 5f;
|
||||
|
||||
//活动范围
|
||||
public float range = 500f;
|
||||
|
||||
//侦查范围
|
||||
public float detectRange = 200f;
|
||||
|
||||
//攻击范围
|
||||
public float attackRange = 5f;
|
||||
|
||||
//角色状态
|
||||
private State state = State.Alert;
|
||||
|
||||
//攻击目标
|
||||
public GameObject target;
|
||||
|
||||
//当前目标点
|
||||
private Vector3 targetPos;
|
||||
|
||||
//初始位置坐标
|
||||
private Vector3 startPos;
|
||||
|
||||
//动画控制器
|
||||
private Animator animator;
|
||||
|
||||
// 角色控制器
|
||||
private CharacterController controller;
|
||||
|
||||
//状态枚举
|
||||
public enum State
|
||||
{
|
||||
//警戒巡逻
|
||||
Alert,
|
||||
|
||||
//追踪状态
|
||||
Track,
|
||||
|
||||
//攻击状态
|
||||
Attack
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
//初始化
|
||||
animator = GetComponent<Animator>();
|
||||
controller = GetComponent<CharacterController>();
|
||||
//记录初始坐标
|
||||
startPos = transform.position;
|
||||
randomTargetPos();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
//攻击状态直接返回
|
||||
if (state == State.Attack)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//角色在攻击状态则返回
|
||||
if (state == State.Alert)
|
||||
{
|
||||
//移动
|
||||
move(targetPos - transform.position);
|
||||
//到达目标点时候随机目标点
|
||||
if (Vector3.Distance(transform.position, targetPos) < 0.1f)
|
||||
{
|
||||
randomTargetPos();
|
||||
}
|
||||
}
|
||||
|
||||
//获取侦查范围内除自己以外的Player层的单位
|
||||
var players = Physics.OverlapSphere(transform.position, detectRange, LayerMask.GetMask("Player"));
|
||||
//如果存在单位
|
||||
if (players.Length > 0)
|
||||
{
|
||||
//找出标签是Friend的,并且不是自己
|
||||
foreach (var player in players)
|
||||
{
|
||||
if (player.tag == "Friend" && player != gameObject)
|
||||
{
|
||||
//进入追逐状态
|
||||
state = State.Track;
|
||||
float dis = Vector3.Distance(transform.position, player.transform.position);
|
||||
//和目标的距离在攻击范围外则朝目标移动
|
||||
if (dis > attackRange)
|
||||
{
|
||||
move(player.transform.position - transform.position);
|
||||
//打印动画speed的值
|
||||
Debug.Log(animator.GetInteger("speed"));
|
||||
}
|
||||
else
|
||||
{
|
||||
attack(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//攻击
|
||||
private void attack(Collider player)
|
||||
{
|
||||
//转向攻击目标
|
||||
transform.rotation = Quaternion.LookRotation(player.transform.position - transform.position, Vector3.up);
|
||||
//攻击动画动作
|
||||
animator.SetTrigger("attack");
|
||||
state = State.Attack;
|
||||
}
|
||||
|
||||
|
||||
// 在 Animator 的 Attack 动画结束时调用此方法
|
||||
public void OnAttackAnimationEnd()
|
||||
{
|
||||
//打印
|
||||
Debug.Log("攻击结束");
|
||||
state = State.Alert;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//角色移动,持续到目标点
|
||||
void move(Vector3 movement)
|
||||
{
|
||||
//改变动画控制器的参数
|
||||
animator.SetInteger("speed", 1);
|
||||
//向目标点移动
|
||||
controller.Move(movement.normalized * Time.deltaTime * speed);
|
||||
//改变角色朝向目标点,只改变y轴
|
||||
transform.rotation = Quaternion.LookRotation(movement, Vector3.up);
|
||||
transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
|
||||
}
|
||||
|
||||
|
||||
//随机目标点
|
||||
private void randomTargetPos()
|
||||
{
|
||||
//以startPos为中心,range为半径,y为0的圆形内随机一个点复制到targetPos
|
||||
targetPos = new Vector3(Random.Range(-range, range), 0, Random.Range(-range, range));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: XXMe5HurAi4+5pK1914zQpj7IOwp0v4frn/tNZC60MwZu6o4g8xPXNM=
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TurnArouundTest : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed = 1f; // 移动速度
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
// 获取点击输入
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
// 获取点击位置
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hit;
|
||||
|
||||
// 检测点击位置是否有物体
|
||||
if (Physics.Raycast(ray, out hit))
|
||||
{
|
||||
// 获取点击位置的坐标
|
||||
Vector3 targetPosition = hit.point;
|
||||
|
||||
// 计算移动方向
|
||||
Vector3 movement = targetPosition - transform.position;
|
||||
movement.y = 0; // 确保只在水平面上旋转
|
||||
|
||||
//改变角色朝向
|
||||
transform.rotation = Quaternion.LookRotation(movement, Vector3.up);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: CSwZsCr4AnnY5jPaLx6eMuFH0B6IHZji5a53j+XdfqSY7cR44j8Hq9Q=
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user