你如何在 C# 中制作向玩家射击的光线投射? [关闭]
Posted
技术标签:
【中文标题】你如何在 C# 中制作向玩家射击的光线投射? [关闭]【英文标题】:How do you make raycasts that shoot at the player in C#? [closed] 【发布时间】:2021-05-31 09:51:09 【问题描述】:我一直在尝试做一个 fps 游戏,但卡在了敌人可以射击玩家的部分,我不太确定我能做什么。敌人继续漫游,而不是实际攻击玩家。更糟糕的是,他们有时会远离玩家。有人可以帮忙吗?
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
public NavMeshAgent NavAgent;
public Transform Player;
public LayerMask GroundCheck, PlayerCheck;
public Transform EnemyPrototype;
public float range;
public float Damage;
public Vector3 WalkPoint; // Code for patrolling Around
bool WalkPointSet;
public float WalkPointRange;
public float SightRange, AttackRange; // Code for checking when to engage in a firefight with the player
public bool PlayerInSight, AttackPlayer;
public float AttackCooldown; // Code for firefights with the player
bool AlreadyAttacked;
private void AIActive()
Player = GameObject.Find("Player").transform;
NavAgent = GetComponent<NavMeshAgent>();
private void Update()
PlayerInSight = Physics.CheckSphere(transform.position, SightRange, PlayerCheck);
AttackPlayer = Physics.CheckSphere(transform.position, AttackRange, PlayerCheck);
if (!PlayerInSight && !AttackPlayer)
Patrol();
if (PlayerInSight && !AttackPlayer)
Engage();
if (PlayerInSight && AttackPlayer)
FireAtPlayer();
private void Patrol() // The enemy will move in order to try and find the player
if (!WalkPointSet)
SearchWalkPoint();
if (WalkPointSet)
NavAgent.SetDestination(WalkPoint);
Vector3 WalkPointDistance = transform.position - WalkPoint;
if (WalkPointDistance.magnitude < 1f)
WalkPointSet = false;
private void SearchWalkPoint() // Makes a random point where the enemy would patrol
float WalkZ = Random.Range(-WalkPointRange, WalkPointRange);
float WalkX = Random.Range(-WalkPointRange, WalkPointRange);
WalkPoint = new Vector3(transform.position.x + WalkX, transform.position.y, transform.position.z + WalkZ);
if (Physics.Raycast(WalkPoint, -transform.up, 2f, GroundCheck))
WalkPointSet = true;
private void Engage() // The enemy has found the player and will move towards them to get a better shot
NavAgent.SetDestination(Player.position);
private void FireAtPlayer() // The enemy will now try to get a shot on the player
NavAgent.SetDestination(transform.position);
transform.LookAt(Player);
if (!AlreadyAttacked)
RaycastHit Hit;
if (Physics.Raycast(EnemyPrototype.transform.position, EnemyPrototype.transform.forward, out Hit, range, PlayerCheck))
Debug.Log(Hit.collider.name);
Target target = Hit.transform.GetComponent<Target>();
if (target != null)
target.TakeDamage(Damage);
AlreadyAttacked = true;
Invoke(nameof(AttackReset), AttackCooldown);
private void AttackReset()
AlreadyAttacked = false;
我不知道问题出在巡逻还是实际的射击代码,因为我对 C# 还很陌生,并且主要使用互联网上的教程来帮助我,但这次我不知道在哪里去
【问题讨论】:
【参考方案1】:有很多事情需要检查。 PlayerInSight
或 AttackPlayer
是否曾经评估为 true
?如果不是,则可能意味着与您的 Player 关联的层不在 PlayerCheck
LayerMask
中。
您可以在检查器中为您的 Player GameObject 分配层:
您可以在检查器中为您的 LayerMask 分配图层:
【讨论】:
感谢您的建议,看来我确实标记了玩家,并且在敌人脚本上确实有玩家层,但玩家没有分配层以上是关于你如何在 C# 中制作向玩家射击的光线投射? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章