Unity的Raycasting对象检测2D的问题
因此,我试图使敌人 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于:
seeplayer是类型的raycasthit2d(struct)。结构永远不会零,因此条件
总是满足的。您可能应该检查内部的对撞机是否有效:
编辑:
我想
也会做这项工作
The problem is in:
SeePlayer is of type RaycastHit2D (a struct). Struct is never null so the condition
is always meet. You should probably check if the collider inside is valid:
EDIT:
I suppose
Would do the job too