- Home /
# FAIL [CLOSED] Why doesn this never return PlayerRay as true?
Why does this never return PlayerRay as true? I am guessing this is the problem since the enemy never follows the player? (It may be a different problem though) Thanks :)
var Player : GameObject;
var PlayerDirection;
var BulletDamage;
private var HeadShotDamage;
var MaxHeadShotDamage : float = 500;
var MinHeadShotDamage : float = 400;
var Ai : GameObject;
var Health : int = 100;
var HealthText : TextMesh;
var MoveSpeed : float = 2.5;
var GunShot : GameObject;
var GunDamage : int;
var GunTimer : float = 0.3;
var MinPlayerDistance : int = 5;
var MaxPlayerDistance : int = 30;
var PlayerClose : boolean;
var PlayerInFov : boolean;
var PlayerRay : boolean;
function Start()
{
PlayerInFov = false;
PlayerRay = false;
}
function Update()
{
Player = GameObject.FindWithTag("Player");
PlayerDirection = Player.transform.position - Ai.transform.position;
BulletDamage = Player.GetComponent("Weapon Fire").Damage;
HeadShotDamage = Random.Range(MinHeadShotDamage, MaxHeadShotDamage);
GunDamage = Random.Range(10, 15);
HealthText.GetComponent(TextMesh).text = Health.ToString();
var PlayerDir = Player.transform.position - transform.position;
Forward = transform.forward;
Angle = Vector3.Angle(PlayerDir, Forward);
var PlayerShot : RaycastHit;
if (Angle < 60)
{
PlayerInFov = true;
}
else
{
PlayerInFov = false;
}
if(Vector3.Distance(transform.position, Player.transform.position) <= MaxPlayerDistance)
{
PlayerClose = true;
}
else
{
PlayerClose = false;
}
if(Physics.Raycast(Ai.transform.position, PlayerDirection, PlayerShot))
{
if(PlayerShot.transform.tag == "Player")
{
PlayerRay = true;
}
else
{
PlayerRay = false;
}
}
if(PlayerClose && PlayerInFov && PlayerRay)
{
transform.LookAt(Vector3(Player.transform.position.x, transform.position.y, Player.transform.position.z));
transform.position += Forward * MoveSpeed * Time.deltaTime;
}
if(Health < 0)
{
Health = 0;
}
if(Health == 0)
{
Destroy(gameObject);
}
}
function OnTriggerEnter(Bullet : Collider)
{
if(Bullet.gameObject.tag == "Bullet")
{
Health -= BulletDamage;
Destroy(Bullet);
}
}
Answer by iwaldrop · Jun 19, 2013 at 07:01 AM
Probably because the Ray isn't pointing at the player, or the player object doesn't have it's tag set correctly. Try the following:
aiPosition : Vector3 = Ai.transform.position;
if(Physics.Raycast(aiPosition, aiPosition - Player.transform.position, PlayerShot))
PlayerRay = PlayerShot.transform.tag.CompareTo("Player");
It does not work :( it does the opposite, it always chases you :(
That's what it sounded like you wanted from the part of your question, "...since the enemy never follows the player". Now you say that 'it' always chases you...the enemy? Isn't that the goal? I guess I'm misunderstanding the question.
Yeah, no the problem is that it seems to never make PlayerRay true, so, when the "if" for the Ai follow you happens, it says "PlayerRay isnt true" so it doesnt play out the actions in the "if" statement? (If that makes any sense?)
FAIL, the players position was too low for it too hit the collider #FAIL, thanks for trying to help though :)
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Object reference not set to an instance of an object 1 Answer
How to toggle a key for a car to go forward or backward? 1 Answer