Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当敌人看到玩家时,让他攻击_C#_Unity3d - Fatal编程技术网

C# 当敌人看到玩家时,让他攻击

C# 当敌人看到玩家时,让他攻击,c#,unity3d,C#,Unity3d,有人能帮我吗?我正在使用下面的代码,试图让敌人看看他是否能看到玩家 但问题是,当我在墙后时,他还能看见我吗 有人能帮我吗 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.SceneManagement; public class Test : MonoBehaviour { [SerializeFi

有人能帮我吗?我正在使用下面的代码,试图让敌人看看他是否能看到玩家

但问题是,当我在墙后时,他还能看见我吗 有人能帮我吗

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;

public class Test : MonoBehaviour
{
    [SerializeField]
    float distance;

    public GameObject player;
    public Transform Shootpoint;

    public bool CanSee = false;

    [SerializeField]
    float chaseDistence = 0.5f;



    void Update()
    {
        distance = Vector3.Distance(Shootpoint.position, player.transform.position);

        if (!Physics.Raycast(Shootpoint.position, player.transform.position, chaseDistence))
        {
            Debug.DrawLine(Shootpoint.position, player.transform.position, Color.red);
            CanSee = false;
            Debug.Log("CANT SEE");

        }
        else if (Physics.Raycast(Shootpoint.position, player.transform.position, chaseDistence))
        {
            Debug.DrawLine(Shootpoint.position, player.transform.position, Color.green);
            CanSee = true;
            Debug.Log("CAN SEE");
        }
    }
}

就像上面提到的@BugFinder一样,您只是在检查
RayCast
是否与某个东西发生了碰撞,而不一定与播放器发生了碰撞

如果要确定光线是否仅与播放器碰撞,而不是与其他对象碰撞,请使用设置
RaycastHit
对象的函数重载。然后你可以得到
标签
,并将其与玩家的标签进行比较。(见和)

你可以这样做:

RaycastHit hitInfo;
if(Physics.Raycast(Shootpoint.position, player.transform.position, out hitInfo, chaseDistence)) 
{
    if (hitInfo.collider.CompareTag("player"))
    {
        // Can see
    }
    else 
    {
        // Can't see, raycast hit something
    }
}
else
{
    // Can't see, raycast hit nothing
}

就像上面提到的@BugFinder一样,您只是在检查
RayCast
是否与某个东西发生了碰撞,而不一定与播放器发生了碰撞

如果要确定光线是否仅与播放器碰撞,而不是与其他对象碰撞,请使用设置
RaycastHit
对象的函数重载。然后你可以得到
标签
,并将其与玩家的标签进行比较。(见和)

你可以这样做:

RaycastHit hitInfo;
if(Physics.Raycast(Shootpoint.position, player.transform.position, out hitInfo, chaseDistence)) 
{
    if (hitInfo.collider.CompareTag("player"))
    {
        // Can see
    }
    else 
    {
        // Can't see, raycast hit something
    }
}
else
{
    // Can't see, raycast hit nothing
}

您没有检查raycast命中的内容,只是它命中了某些内容……您可以告诉我如何检查它吗?此外,执行两次raycast是多余的,只会浪费性能;)您没有检查raycast命中的内容,只是它命中了某些内容……您可以告诉我如何检查它吗?此外,执行两次raycast是多余的,只会浪费性能;)