🎮
虚拟相机之FreeLook
适用于目前所有的第三人称视角的摄像机
需要把原有的虚拟相机关闭
去修改攻击范围 尽量不要使用hardCode
高亮注释
//TODO: 内容
Rider我是没找到这个插件
敌人的追击
新学了一个API 👉Physics.OverlapSphere()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public enum EnemyStates { GUARD, PATROL, CHASE, DEAD }
[RequireComponent(typeof(NavMeshAgent))] public class EnemyController : MonoBehaviour { private NavMeshAgent agent; private EnemyStates enemyStates;
[Header("Basic Settings")] public float sightRadius;
private void Awake() { agent = GetComponent<NavMeshAgent>(); }
private void Update() { SwitchStates(); }
void SwitchStates() { if (FoundPlayer()) { enemyStates = EnemyStates.CHASE; print("找到玩家"); } switch (enemyStates) { case EnemyStates.GUARD: break; case EnemyStates.PATROL: break; case EnemyStates.CHASE: break; case EnemyStates.DEAD: break; } }
bool FoundPlayer() { var colliders = Physics.OverlapSphere(transform.position, sightRadius); foreach (var target in colliders) { if (target.CompareTag("Player")) { return true; } }
return false; } }
|
13 敌人可以追着玩家走
//更改动画播放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public enum EnemyStates { GUARD, PATROL, CHASE, DEAD }
[RequireComponent(typeof(NavMeshAgent))] public class EnemyController : MonoBehaviour { private NavMeshAgent agent; private EnemyStates enemyStates; private Animator anim; [Header("Basic Settings")] public float sightRadius;
private float speed; public bool isGuard; private GameObject attackTarget;
private bool isWalk; private bool isChase; private bool isFollow; private void Awake() { anim = GetComponent<Animator>(); agent = GetComponent<NavMeshAgent>(); speed = agent.speed; }
private void Update() { SwitchStates(); SwitchAnimation();
}
void SwitchAnimation() { anim.SetBool("Walk",isWalk); anim.SetBool("Chase",isChase); anim.SetBool("Follow",isFollow); }
void SwitchStates() { if (FoundPlayer()) { enemyStates = EnemyStates.CHASE; } switch (enemyStates) { case EnemyStates.GUARD: break; case EnemyStates.PATROL: break; case EnemyStates.CHASE: isWalk = false; isChase = true; agent.speed = speed; if (!FoundPlayer()) { isFollow = false; agent.destination = transform.position; } else { isFollow = true; agent.destination = attackTarget.transform.position; } break; case EnemyStates.DEAD: break; } }
bool FoundPlayer() { var colliders = Physics.OverlapSphere(transform.position, sightRadius); foreach (var target in colliders) { if (target.CompareTag("Player")) { attackTarget = target.gameObject; return true; }
} attackTarget = null; return false; } }
|