【发布时间】:2014-12-28 02:29:57
【问题描述】:
谁能告诉我代码有什么问题? 如果你能看到,只要玩家在光线投射的范围内。敌人变成红色。 但出于某种原因。即使玩家已经满足了让敌人变红的要求。它变回白色。这仅在您手动移动时可见,在正常速度下它看起来好像在闪烁。
我认为发生这种情况的原因是,其中一个光线投射不再满足要求。大概与此有关。我不知道确切。所以请帮我解决这个问题。
提前致谢。
这张图显示,即使玩家在使敌人变红的要求范围内。它没有。为什么?
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;
}
}
【问题讨论】:
-
您的部分脚本丢失,即 Player_Transform_Points_Detection() 方法...
标签: c# vector unity3d detection raycasting