-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprojectileMove.cs
More file actions
34 lines (26 loc) · 989 Bytes
/
Copy pathprojectileMove.cs
File metadata and controls
34 lines (26 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class projectileMove : MonoBehaviour
{
Rigidbody2D rb;
public GameObject explosionPrefab;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.AddForce(transform.right * 200f);
}
private void OnCollisionEnter2D(Collision2D collision){
if(collision.gameObject.CompareTag("Asteroid")){
// finds where to instantiate
Vector2 instantiatePoint = collision.GetContact(0).point;
// instantiate the explosion prefab
Instantiate(explosionPrefab, instantiatePoint, Quaternion.identity);
// Destroy asteroid
Destroy(collision.gameObject);
// destroy the missile (the gameObject that this script is attached to) (last thing to happen)
Destroy(gameObject);
}
}
}