I have an object tagged “Hurt” that will take 1 health point away from the player object.
I want that same object to hurt an enemy that chases the player, so that the player
can use the object against the enemy by leading the enemy into said object.
Here is my playerController script:
#pragma strict
var speed : float = 4;
var jumpHeight : float = 5;
var superSpeed : float = 10;
var superSpeedTime : float = 5;
var health : int = 5;
private var superSpeedOn : boolean = false;
private var superSpeedElapsed : float = 0;
function FixedUpdate () {
//Handle the horizontal movement of the player
if(superSpeedOn == true) {
rigidbody.velocity.x = superSpeed * Input.GetAxis("Horizontal");
//Add to elapsed time
superSpeedElapsed += Time.fixedDeltaTime;
//Check if enough time has passed
if(superSpeedElapsed >= superSpeedTime) {
superSpeedOn = false;
superSpeedElapsed = 0;
}
}
else {
rigidbody.velocity.x = speed * Input.GetAxis("Horizontal");
}
//Handle the jumping for the player
if( Input.GetButton("Jump") && IsGrounded() ) {
rigidbody.velocity.y = jumpHeight;
}
//Check if we are hitting the side of an object
var distance : float = rigidbody.velocity.magnitude * Time.fixedDeltaTime;
var hit : RaycastHit;
if(rigidbody.SweepTest(rigidbody.velocity, hit, distance) && !IsGrounded) {
//Stop Moving!
rigidbody.velocity.x = 0;
}
}
function IsGrounded () {
//Fire a raycast to check for the ground
return Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y + 0.01);
}
function Hit () {
//Remove one hit point and check if we're dead
health -= 1;
if(health <= 0) {
Debug.Log("Oh shit, you're dead!");
Destroy(gameObject);
}
}
function SuperSpeed () {
//Turn on superSpeed
superSpeedOn = true;
}
function OnCollisionEnter(collision : Collision) {
//Check if we hit an object that can hurt us
if(collision.gameObject.tag == "Hurt") {
Hit();
}
}
I used the same code that hurts the player in my ChasingEnemy script, but the enemy does not take damage, nor does the script ever actually check to see if his health has been reduced to 0. For example if I reduce his health parameter to zero myself, the object is still not destroyed. Here is the ChasingEnemy script:
#pragma strict
var speed : float = 1.8;
var chaseAt : float = 5;
var enemyHealth : int = 1;
private var player : GameObject;
private var startingY : float;
function Start () {
//Find and remember the player
player = GameObject.Find("Player");
startingY = transform.position.y;
}
function Update () {
//Calculate the distance between the player and the enemy
var distance = Vector3.Distance(transform.position, player.transform.position);
//Check how close we are
if(distance <= chaseAt) {
//Chase the player
transform.Translate( (player.transform.position - transform.position) * speed * Time.deltaTime);
transform.position.y = startingY;
}
}
function Hit () {
//Remove one hit point and check if we're dead
enemyHealth -= 1;
if(enemyHealth <= 0) {
Destroy(gameObject);
}
}
function OnCollisionEnter(collision : Collision) {
//Check if we hit an object that can hurt us
if(collision.gameObject.tag == "Hurt") {
Hit();
}
}
I’m only mildly experienced with Java and I’ve been working with Unity for less than a week, so I apologize if this is a very “noobish” question. Most of the code came from a tutorial video but the guy who made the video explained the code very well, so I feel like I have a decent understanding of how it all works. I’m definitely missing something though! Any help would be greatly appreciated. Thanks!