Unity的Raycasting对象检测2D的问题

发布于 2025-02-09 23:08:17 字数 2314 浏览 2 评论 0原文

因此,我试图使敌人 ai 检测到unity raycaster 的球员。 ai 在工作时,即使玩家的里程比我想要的更远,也可以检测到玩家的里程。你能帮我吗? 另外,我得到了arigidbody2d和一个播放器目标。 seedistance是2个

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyMove : MonoBehaviour
{
    public int speed;
    private bool movingRight = true;
    public bool SeenPlayer = false;
    public Transform target;
    public float seeDistance;
    public int health = 3;
    public Rigidbody2D rb;

    public Transform groundDetection; 

    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        rb = GetComponent<Rigidbody2D>();
        
    }

    void Update()
    {

        if (SeenPlayer == false)
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        else
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
            transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }

        RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2f);
        RaycastHit2D SeePlayer = Physics2D.Raycast(groundDetection.position, Vector2.left, seeDistance);
        if (groundInfo.collider == false)
        {
            if (movingRight == true)
            {
                transform.eulerAngles = new Vector3(0, -180, 0);
                movingRight = false;
            }
            else
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                movingRight = true;
            }
        }
        if (SeePlayer)
        {
            SeenPlayer = true;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        BulletScript bullet = collision.GetComponent<BulletScript>();
        if (bullet != null)
        {
            //Destroy(gameObject);-
            health -= 1;
            Destroy(bullet.gameObject);
            if (health == 0)
            {
                Destroy(gameObject);
            }
            else
            {
                rb.AddForce(new Vector2(4, 2), ForceMode2D.Impulse);
            }
        }
    }
}

So im trying to make an enemy AI which detects a player with the unity raycaster. The AI is, while working, detecting the player even if its miles further away than I would like. Could you help me?
also I'm getting a Rigidbody2D and a player target. The seeDistance is 2

code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyMove : MonoBehaviour
{
    public int speed;
    private bool movingRight = true;
    public bool SeenPlayer = false;
    public Transform target;
    public float seeDistance;
    public int health = 3;
    public Rigidbody2D rb;

    public Transform groundDetection; 

    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        rb = GetComponent<Rigidbody2D>();
        
    }

    void Update()
    {

        if (SeenPlayer == false)
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        else
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
            transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }

        RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2f);
        RaycastHit2D SeePlayer = Physics2D.Raycast(groundDetection.position, Vector2.left, seeDistance);
        if (groundInfo.collider == false)
        {
            if (movingRight == true)
            {
                transform.eulerAngles = new Vector3(0, -180, 0);
                movingRight = false;
            }
            else
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                movingRight = true;
            }
        }
        if (SeePlayer)
        {
            SeenPlayer = true;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        BulletScript bullet = collision.GetComponent<BulletScript>();
        if (bullet != null)
        {
            //Destroy(gameObject);-
            health -= 1;
            Destroy(bullet.gameObject);
            if (health == 0)
            {
                Destroy(gameObject);
            }
            else
            {
                rb.AddForce(new Vector2(4, 2), ForceMode2D.Impulse);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2025-02-16 23:08:17

问题在于:

if(SeePlayer){
    SeenPlayer = true;
}

seeplayer是类型的raycasthit2d(struct)。结构永远不会零,因此条件

if(SeePlayer)

总是满足的。您可能应该检查内部的对撞机是否有效:

if(SeePlayer.collider)

编辑:
我想

if(SeePlayer != default(RaycastHit2D))

也会做这项工作

The problem is in:

if(SeePlayer){
    SeenPlayer = true;
}

SeePlayer is of type RaycastHit2D (a struct). Struct is never null so the condition

if(SeePlayer)

is always meet. You should probably check if the collider inside is valid:

if(SeePlayer.collider)

EDIT:
I suppose

if(SeePlayer != default(RaycastHit2D))

Would do the job too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文