I’m trying to create a raycast for my enemy so if the player is behind a wall, the enemy will not attack…This is my full enemy script with raycast implemented inside :
var lookAtTarget : Transform;
var runningspeed : int;
var speed : int;
var range : int;
var damp : float;
var ENEMY_HEALTH = 30;
var nextFire = 0.0;
private var fireRate = .2;
var enemyBullet : Transform;
var death : Transform ;
var health : Transform;
function Awake()
{
if (!lookAtTarget)
{
lookAtTarget = GameObject.FindWithTag("Player").transform;
ENEMY_HEALTH = 30;
}
}
function Update()
{
Debug.Log(ENEMY_HEALTH);
if(ENEMY_HEALTH <= 0)
{
Instantiate(death, transform.position, transform.rotation);
Instantiate(health, transform.position, transform.rotation);
Debug.Log("DEAD");
Destroy(gameObject);
}
var dist = lookAtTarget.position - transform.position;
var hit : RaycastHit;
if(Physics.Raycast(transform.position, dist,hit))
{
if(hit.collider.CompareTag("Player"))
{
Attack();
}
else
{
return false;
}
Debug.DrawLine (transform.position, hit.point);
}
if ( Vector3.Dot(transform.forward.normalized, (lookAtTarget.position - transform.position).normalized) > .9 Attack())
{
if(Time.time >= nextFire)
{
fire();
nextFire = Time.time + fireRate;
}
}
}
function Attack()
{
var rotate = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
transform.Translate (0,0,runningspeed*Time.deltaTime);
return true;
}
function fire()
{
var bullet = Instantiate(enemyBullet, GameObject.FindWithTag("shootpoint").transform.position, transform.rotation);
bullet.rigidbody.velocity = transform.forward * speed;
}
function OnParticleCollision ()
{
Debug.Log("hit");
ENEMY_HEALTH--;
}
The problem im having is that when i play the game, if the player is standing still and in range of the enemy, the enemy will not attack…but if i start moving, thats when the enemy starts to attack…and again, once i stop, the enemy stops attacking me…All im trying to get accomplished is to have the enemy disengage when the player is either behind a wall or out of range with the use of raycast…I also tried using linecast and i was getting the same result…
Maybe there’s some round-off error with regards to your distance. You do your raycast at the exact distance between the player and the enemy, so maybe increase the distance limit some.
Oh no, disregard what I said. I was thinking of a different version of Raycast. What you have there I think should work. I don’t know what the problem is.
What are you trying to do check this line?
Vector3.Dot(transform.forward.normalized, (lookAtTarget.position - transform.position).normalized) > .9
I was using that code to detect when the enemy is facing the player…therefore, when the enemy starts to rotate towards the player, they aren’t constantly shooting.
and i stillllll can’t figure out why this raycast is not working :\
I also tried inserting the rotating moving code directly in the IF statement rather then calling a function and im getting the same result
Put a debug.log in after the raycast call; see if it’s the raycast call that’s failing or if it’s just a logical bug in your code. If the raycast is passing, then it’s just an oversight in your logic. If the raycast is failing, then maybe something is up.
EDIT: also log out the values going into the raycast (transform.position, dist) and validate that they’re the numbers that you expect to have.
I appreciate your help and please don’t mind my noob response since im new to all of this…but if the “attack” function is getting called, then the raycast is definitely being initiated…the enemy moves towards me ONLY IF the player is in motion
EDIT: I put a debug.log after the if statement and then another after the else…when my player is standing still, the raycast jumps to the else statement…same result but seems like the raycast is working…
I think u should change:
if ( Vector3.Dot(transform.forward.normalized, (lookAtTarget.position - transform.position).normalized) > .9 Attack())
to:
if (Attack() Vector3.Dot(transform.forward.normalized, (lookAtTarget.position - transform.position).normalized) > .9)
maybe when Vector3.Dot(transform.forward.normalized, (lookAtTarget.position - transform.position).normalized) > .9 is not true engine skip the attak() statement and the character dosnt rotate to target
I tried that but im still getting the same result…its getting a little aggravating. I thought it would be simple but their is obviously something very stupid thats causing this to happen…