Unity远程敌人Agro功能失效问题排查(变量已赋值)
Unity远程弓箭敌人Agro脚本移动故障排查
我正在Unity中开发一款游戏,远程弓箭敌人的Agro脚本出现故障:当敌人与玩家距离超出射击范围时,不会向玩家移动靠近以进入可射击距离。所有调试打印语句均能在控制台正常显示,且已为代码中的所有变量赋值,同时附上了代码参数配置截图。我曾尝试将else语句改为明确判断距离大于射击距离,并添加调试打印,但问题仍未解决。

对应的C#代码如下:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class RangedAgro : MonoBehaviour { public float distanceToShoot; public float distance; private Transform player; public Rigidbody2D rb; public int speed; public GameObject bullet; public Transform bulletPos; private float timer; // Start is called before the first frame update void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame void Update() { timer += Time.deltaTime; distance = Vector2.Distance(transform.position, player.position); if (distance <= distanceToShoot) { rb.velocity = Vector2.zero; if(timer > 2){ timer = 0; Shoot(); } } else if(distance > distanceToShoot) { Chase(); print("TRUE"); } if(player.transform.position.x > transform.position.x) transform.eulerAngles = new Vector3(0, 0, 0); if(player.transform.position.x < transform.position.x) transform.eulerAngles = new Vector3(0, 180, 0); } void Shoot() { Instantiate(bullet, bulletPos.position, Quaternion.identity); } void Chase() { if (player.position.x > transform.position.x) { rb.velocity = new Vector2(speed, 0); transform.eulerAngles = new Vector3(0, 0, 0); print("YES"); }else if (player.position.x < transform.position.x) { rb.velocity = new Vector2(-speed, 0); transform.eulerAngles = new Vector3(0, 180, 0); print("NO"); } } }
我曾尝试通过明确判断距离大于射击距离来优化else语句,并添加调试打印语句排查问题。
内容的提问来源于stack exchange,提问作者TurtleLover69
相关产品推荐
相关产品推荐

