This question was
closed Jul 23, 2016 at 07:43 PM by
M-Mahad for the following reason:
Other
Question by
M-Mahad · Jul 22, 2016 at 01:55 AM ·
triggercollider 2dtriggering animation box collider
How do i make my arrow hit an enemy and instantiate a blood sprite in its place?
I have researched about this by myself but I have always gotten confused between which to use,ontriggerenter or oncollisonenter?And how to use them?Can someone please shed light on this?
Here is my arrow code:
> using UnityEngine; using
> System.Collections;
>
> public class PlayerArrowUp :
> MonoBehaviour {
>
> //speed of bullet
> float speed;
>
> // Use this for initialization
> void Start()
> {
>
>
> speed = 2f;
> }
>
>
>
> // Update is called once per frame
> void Update()
> {
>
> //Get the bullets current positon
> Vector2 position = transform.position;
>
> //compute the bullets new position
> position = new Vector2(position.x, position.y + speed
> * Time.deltaTime);
>
> // Update the bullets position
> transform.position = position;
>
> Vector2 max = Camera.main.ViewportToWorldPoint(new
> Vector2(1, 1));
>
> //If the bullet went outside the screen,the destroy the bullet
> if (transform.position.y > max.y)
>
> Destroy(gameObject);
>
> } }
And here is my enemy code:
> using UnityEngine; using
> System.Collections;
>
> public class
> Monster_Movement_Downwards :
> MonoBehaviour {
> public GameObject Monster_Blood;
> public int life = 0;
> //speed of bullet
> float speed;
>
> // Use this for initialization
> void Start()
> {
>
>
> speed = 1f;
> }
>
>
>
>
>
>
>
> // Update is called once per frame
> void Update()
> {
>
>
>
>
>
>
>
> //Get the monster's current positon
> Vector2 position = transform.position;
>
> //compute the monster's new position
> position = new Vector2(position.x, position.y + speed
> * -Time.deltaTime);
>
> // Update the monster's position
> transform.position = position;
>
> Vector2 max = Camera.main.ViewportToWorldPoint(new
> Vector2(1, 1));
>
> //If the monster went outside the screen,the destroy the monster
> if (transform.position.y > max.y)
>
> Destroy(gameObject);
>
> }
>
>
> }
Comment