在 C# 中如何制作向播放器射击的光线投射?

How do you make raycasts that shoot at the player in C#?

我一直在尝试做一个 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# 很陌生,主要使用互联网上的教程来帮助我,但这次我不知道去哪里

有很多事情要检查。 PlayerInSightAttackPlayer 的计算结果是否为 true?如果不是,则可能意味着与您的播放器关联的图层不在 PlayerCheck LayerMask.

您可以在检查器中为您的玩家游戏对象分配图层:

并且您可以在检查器中为您分配 LayerMask 图层: