- Home /
OnTriggerEnter
I need some help with onTriggerEnter, I want to be able to have attack the enemy (with Input.GetMouseButtonDown(0) and also need to have to have a collision with the ShortSword game object, but it is not working! I want my enemy to loose health if he gets touched by the ShortSword and mousebutton is down. Also I am working in 2D. thx for the help :)
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float Health = 100;
public GameObject ShortSword;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0) && OnTriggerEnter(ShortSword))
{
Health=Health - 10;
Debug.Log(Health);
}
}
}
Answer by Dizzyman572 · Jun 12, 2014 at 02:27 AM
Since your working in 2D, your gonna want to use the function OnTriggerEnter2D. Here it is in the Unity Scripting Reference. http://docs.unity3d.com/ScriptReference/Collider2D.OnTriggerEnter2D.html
And this tutorial may help you gain understand on how you use the OnTrigger functions. http://unity3d.com/learn/tutorials/projects/space-shooter/creating-hazards
Hope this helps. :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
OnTriggerEnter2D Box Collider 1 Answer
Distribute terrain in zones 3 Answers
Is there a way to find other's components using OnTriggerEnter? 1 Answer
AI Attack Not Working! W/Video 1 Answer