谁能告诉我代码有什么问题? 如果您可以看到,只要玩家处于光线投射的范围内。敌人变红了。 但出于某种原因。即使玩家满足要求让敌人变红的要求。它变回白色。这只有在您手动移动时才能看到,正常速度下它看起来好像在闪烁。
我认为发生这种情况的原因是,其中一个光线投影不再符合要求。可能与此有关。我完全不知道。所以请帮我解决这个问题。
提前致谢。
这张照片显示,即使玩家处于使敌人变红的要求之内。它没有。为什么?
using UnityEngine;
using System.Collections;
/* TL = Top Left
* TR = Top Right
* BL = Bottom Left
* BR = Bottom Right
*/
public class Script_v2 : MonoBehaviour {
// Player Properties
private GameObject player;
public Vector3 playerSize;
private Vector3 playerTransform;
public Vector3 playerTransformTL;
public Vector3 playerTransformTR;
public Vector3 playerTransformBL;
public Vector3 playerTransformBR;
private Vector3 newPlayerTransformTL;
private Vector3 newPlayerTransformTR;
private Vector3[] playerRaycastPoints;
// Enemy Properties
private Vector3 enemyTransformTL;
private Vector3 enemyTransformTR;
private Vector3 enemyTransformBL;
private Vector3 enemyTransformBR;
public float distance;
public Vector3 enemySize;
// Detection Alerts
public bool alerted;
public bool alertedLock;
public bool dead;
Ray ray;
RaycastHit hit;
// Use this for initialization
void Start () {
playerRaycastPoints = new Vector3[4];
distance = 3f;
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
enemyTransformTL = new Vector3 (transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z);
enemyTransformTR = new Vector3 (transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z);
enemyTransform_TL_TR ();
detectionAlert ();
Reference_Player_Transform_Points ();
Player_Transform_Points_Detection ();
}
void OnDrawGizmos() {
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere (new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
Gizmos.DrawWireSphere (new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
}
public void enemyTransform_TL_TR() {
for (int i = 0; i < 4; i++) {
double enemyAngleTL = Mathf.Atan2(playerRaycastPoints[i].y - ( transform.position.y + 0.5f ),
playerRaycastPoints[i].x - ( transform.position.x - 0.5f )) * 180f / 3.14159265f;
Debug.Log (enemyAngleTL);
double enemyAngleTR = Mathf.Atan2 (playerRaycastPoints[i].y - (transform.position.y + 0.5f),
playerRaycastPoints[i].x - (transform.position.x + 0.5f)) * 180f / 3.14159265f;
Vector3 directionTL = (playerRaycastPoints[i] - enemyTransformTL).normalized;
Ray rayTL = new Ray(enemyTransformTL, directionTL);
RaycastHit hitTL;
Vector3 directionTR = (playerRaycastPoints[i] - enemyTransformTR).normalized;
Ray rayTR = new Ray (enemyTransformTR, directionTR);
RaycastHit hitTR;
//Debug.DrawRay (rayTR.origin, rayTR.direction * distance, Color.yellow);
if(Physics.Raycast (rayTL, out hitTL, distance)) {
if((enemyAngleTL > 90 && enemyAngleTL < 180)) {
Debug.DrawRay (rayTL.origin, rayTL.direction * distance, Color.yellow);
alerted = true;
}
}
else {
alerted = false;
}
}
}
public void detectionAlert() {
if (alerted == true) {
gameObject.renderer.material.color = Color.red;
}
else {
gameObject.renderer.material.color = Color.white;
}
}
private void Reference_Player_Transform_Points() {
playerSize = player.transform.localScale;
playerTransformTL = new Vector3(player.transform.position.x - (playerSize.x / 2),
player.transform.position.y + playerSize.y / 2,
player.transform.position.z);
playerTransformTR = new Vector3(player.transform.position.x + (playerSize.x / 2),
player.transform.position.y + playerSize.y / 2,
player.transform.position.z);
playerTransformBL = new Vector3(player.transform.position.x - (playerSize.x / 2),
player.transform.position.y - playerSize.y / 2,
player.transform.position.z);
playerTransformBR = new Vector3(player.transform.position.x + (playerSize.x / 2),
player.transform.position.y - playerSize.y / 2,
player.transform.position.z);
playerRaycastPoints [0] = playerTransformTL;
playerRaycastPoints [1] = playerTransformTR;
playerRaycastPoints [2] = playerTransformBL;
playerRaycastPoints [3] = playerTransformBR;
}
}
答案 0 :(得分:0)
我没有彻底阅读整个事情,但我闻到了一些可疑的东西,这里是:你的状态(颜色)取决于“警告”布尔值,它只会在enemyTransform_TL_TR()中受到影响。
你在播放器框的4个顶点(在2D右边?),在for循环中(这是臭部分)。
因此,如果一个演员成功,它将开启警报,但如果for中的下一个失败,那么它将被关闭警报。也许你应该在for循环之前关闭警报,然后在成功的第一个演员阵容打开并打破for?
另外,我不喜欢该演员的外观,你没有处理其中一个案例(参见我的“其他”评论):
if (Physics.Raycast(rayTL, out hitTL, distance))
{
if ((enemyAngleTL > 90 && enemyAngleTL < 180))
{
Debug.DrawRay(rayTL.origin, rayTL.direction * distance, Color.yellow);
alerted = true;
}
// else: what then? should alerted be false? here's you're just leaving it as is...
}
else
{
alerted = false;
}
除此之外,正如我在评论中所说,您的脚本未开箱即用,方法Player_Transform_Points_Detection()
缺失。
并且,您的代码可能会使用一些清理:许多未使用的变量,方法和变量都是公开的。
你应该更多地缓存内容:比如Reference_Player_Transform_Points()
,你正在做player.transform.position
12次!每次都有效地从发动机上拉出位置。只需将其拉出var playerPos = player.transform.position;
一次,然后在任何地方使用playerPos
。整个地方的playerRaycastPoints[i]
也是如此。您的代码将更快,更易读。
你也将玩家大小除以2 4倍,你可以做一次(并且乘以0.5f而不是因为划分是昂贵的,但是这种挑剔可能是编译器会为你做的,无论如何,我'我不确定。)
您可以使用Mathf.Rad2Deg
代替180f / 3.14159265f
,或至少使用Mathf.PI
。
您可以直接初始化字段:private Vector3[] playerRaycastPoints = new Vector3[4];
,而不是在Start()上初始化。
......好的,我现在就停止了;)!