- Home /
Why doesn't this AI script work? c#
hey there,
So I'm creating a object that moves back and forth from 2 points, i wanted to make it if the player touched the gameobject it loses health.
I made an empty gameobject with 3 different spheres attached to it.
Here is the code:
using UnityEngine;
using System.Collections;
public class Enemy2AI : MonoBehaviour {
public Transform StartPoint;
public GameObject Player;
public Transform Endpoint;
public bool DirectionSwap;
public float Speed;
void FixedUpdate () {
if(transform.position == Endpoint.position){
DirectionSwap = true;
}
if(transform.position == StartPoint.position){
DirectionSwap = false;
}
if(DirectionSwap){
transform.position = Vector3.MoveTowards(transform.position,StartPoint.position,Speed);
}
else{
transform.position = Vector3.MoveTowards(transform.position, Endpoint.position,Speed);
}
if(Player.transform.position == gameObject.transform.position){
HealthBar hb = Player.GetComponent<HealthBar>();
hb._Currenthealth -= 10;
}
}
}
Please specify what doesn't work. What do you expect it to do, and what is it doing ins$$anonymous$$d?
You're probably going to want to use Physics colliders to detect collisions (touches): http://docs.unity3d.com/Documentation/$$anonymous$$anual/Physics.html
Sorry, I thought i wrote it out.
The back and forth works, i don't get why it wont deduct health when the position of the player is equal to the object.
It's extremely unlikely that the positions will be exactly the same. See my suggested answer below.
Answer by TonyLi · May 07, 2013 at 08:04 PM
Two vectors in 3D space consisting of 3 floats each will almost never be exactly equal.
Instead, attach colliders to the player and the object.
In a script on one of the objects, include:
void OnCollisionEnter(Collision collision) {
HealthBar hb = Player.GetComponent<HealthBar>();
hb._Currenthealth -= 10;
}
Reference: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
Still doesn't work. I even altered it to make it:
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.name == "Player Controller "){
HealthBar hb = Player.GetComponent<HealthBar>();
hb._Currenthealth -= 10;
}
}
Don't worry I fixed it and used the on trigger func as that worked better for what i needed it for. Anyways thanks for pointing me in the right direction. :)
Try adding some debug lines to see where it's failing:
void OnCollisionEnter(Collision collision) {
Debug.Log("1. Collided with " + collision.gameObject.name);
if (collision.gameObject == Player) {
Debug.Log("2. This is the player.");
HealthBar hb = Player.GetComponent<HealthBar>();
if (hb == null) Debug.Log("3. HealthBar is NULL");
hb._Currenthealth -= 10;
}
}
If you get to Debug #1, you know the collision worked.
Note that I changed the player check conditional in two ways:
Compares the gameObject with Player, since you already have it. This may or may not be correct depending on your implementation.
Got rid of the string comparison. I noticed that in your example code the string has a space at the end -- "Player Controller "
If you get to Debug #2, you know the collision hit the player object.
If you get Debug #3, then it didn't find HealthBar on the player object.
Thanks tony it's working now and polished, you're a star :)
Your answer
Follow this Question
Related Questions
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
Enemy AI C# 0 Answers
Multiple Cars not working 1 Answer