- Home /
About trigger
So, I would like that when I go close enough to the mob, then EnemyAttack script is enabled. I understood that i can do it with trigger. So how i make that when player touch cube or somethin then this code activate: using UnityEngine; using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization void Start () { attackTimer = 0; coolDown = 1.0f;
}
// Update is called once per frame void Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;
if(attackTimer < 0) attackTimer = 0;
if(attackTimer == 0) { Attack(); attackTimer = coolDown; }
} private void Attack() { float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
Debug.Log(direction);
if(distance < 7) { if(direction > 0) { PlayerHealt eh = (PlayerHealt)target.GetComponent("PlayerHealt"); eh.AddjustCurrentHealth(-10); } } }
}
Answer by Doireth · Jul 19, 2012 at 10:47 PM
Check this out, may be what you're looking for.
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
-or-
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
Answer by Maxddualz · Jul 20, 2012 at 01:43 PM
I am a beginner in these things and I can't get this work >.
First, could you format the code above using the "101010" button? Would make it easier to help implement it.
Answer by Mander · Jul 20, 2012 at 03:28 PM
it would be like this in javascript
function OnTriggerEnter (other : Collider) {
gameObject.GetComponent("EnemyAttack").enabled = true; //script true
}
function OnTriggerExit (other : Collider) {
gameObject.GetComponent("EnemyAttack").enabled = false; //script false
}
Your answer
Follow this Question
Related Questions
How do I hurt enemies by jumping on them without getting killed? 1 Answer
Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 Answers
enemy attack the player 0 Answers
How to handle multiple colliders for one enemy and bullet. 3 Answers
I use this script, but the enemy lose health if i don´t target him. 1 Answer