首页 > 解决方案 > 你如何在 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# 很陌生,并且主要使用互联网上的教程来帮助我,但这次我不知道去哪里

标签: c#unity3draycasting

解决方案


有很多事情要检查。是否PlayerInSightAttackPlayer曾经评估为true?如果不是,则可能意味着与您的 Player 关联的层不在PlayerCheck LayerMask.

您可以在检查器中为您的 Player GameObject 分配层:

img:将图层分配给玩家游戏对象

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

img:在 LayerMask 字段中分配图层


推荐阅读