- Home /
GameObjects that destroy themselves when there raycast hits the player
i am trying to build a boss ai. i need it to strafe side to side (which works fine) and move towards the player when he is in range of its raycast but instead it destroys itself as soon as the ray cast hits the player it also instantiates othe enemies that find and move toward the player but they destroy themselves also it only happens on this level and i cant work out why. i am new to unity and i have searched around but i cant find any similar questions here is the boss blocks code
public var moveSpeed : Number = 10.0;
public var moveDirection : Vector3 = Vector3(0,0,1); // Initial movement direction
public var errorComp : Number = 0.95; // This prevents a body of the same size from missing the object it is touching
var life : int = 60;
var target :Transform;
var speed :int = 20;
var hit : RaycastHit;
var distance : float = 10;
private var Direction : Vector3;
private var ray : Ray;
var mask : LayerMask;
function Move()
{
var moveDistance : Number = moveSpeed * Time.deltaTime; // The distance to move this update
var hitInfo : RaycastHit; // The raycast info variable
// Check if there is anything in the way
if (Physics.Raycast(this.transform.position , moveDirection , hitInfo , moveDistance + this.collider.bounds.extents.z))
{
// Reduce the movement distance so the AI doesn't run into the wall
moveDistance = hitInfo.distance - this.collider.bounds.extents.z;
}
// If there is nowhere to move, turn around
if ( moveDistance < 0.001 )
{
// turn around
moveDirection.z = -moveDirection.z;
}
transform.position += moveDirection * moveDistance;
}
function Update () {
Move();
if (target != null)
{
Direction = target.transform.position - transform.position;
}else{target = gameObject.FindWithTag("player").transform;}
ray = Ray( this.transform.position , Direction);
if (Physics.Raycast(transform.position, Direction, distance, mask))
{
Debug.DrawRay( ray.origin , ray.direction * distance , Color.green );
transform.position = Vector3(Mathf.MoveTowards(transform.position.x, target.position.x,Time.deltaTime * speed),0,Mathf.MoveTowards(transform.position.z, target.position.z,Time.deltaTime * speed)); // If it hits the player, move towards it
}
else
{
Debug.DrawRay( ray.origin , ray.direction * distance , Color.red );
transform.position = Vector3(583.6437,this.transform.position.y,this.transform.position.z);
}
}
function damage()
{
life -= 1;
if (life == 0) //only if my life points = 0 will i destroy myself
{
Destroy(gameObject);
}
}
function OnCollisionEnter (other : Collision)
{
if (other.gameObject.CompareTag("player")) //send message to the player telling it to destroy itself
other.gameObject.SendMessage("ApplyDamage");
Destroy(gameObject);// destroy yourself on impact
}
i dont know what is going on
Answer by DanFrias · Jul 19, 2013 at 03:52 AM
The last line of the OnCollisionEnter function effectively destroys the boss when the raycast hits it, as you describe. What is the purpose of that line?
that part i noticed after posting i got rid of it however it still dissapears its they aren't destroyed on collision with the player they are moved off screen i have it set up as a layered level and when they detect the player they move to the upper level any idea how to fix sorry for the mistake
I'm unclear as to what you're saying. The boss moves to another level when it detects the player? What are these layers you are describing? Layers in Unity are used for rendering scenes with multiple cameras, so I'm not sure how that fits into this. Please try to better describe what it is you're seeing or what is occurring, and if the code has changed at all from that which you pasted above.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110420im_/https://answers.unity.com/themes/thub/images/avi.jpg)