-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoconutThrower.cs
More file actions
35 lines (27 loc) · 1.06 KB
/
CoconutThrower.cs
File metadata and controls
35 lines (27 loc) · 1.06 KB
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
35
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class CoconutThrower : MonoBehaviour {
// Handles the instantiation and behavoir of coconut projectlies in coconut throw game.
public AudioClip throwSound;
public Rigidbody coconutPrefab;
public float throwSpeed = 30.0f;
public static bool canThrow = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// checks to see if player can throw coconut before running routine
if(Input.GetButtonDown("Fire1") && canThrow){
audio.PlayOneShot(throwSound);
// creates coconut
Rigidbody newCoconut = Instantiate(coconutPrefab,transform.position, transform.rotation) as Rigidbody;
newCoconut.name = "coconut";
// gives coconut velocity
newCoconut.velocity = transform.forward * throwSpeed;
// stops Rigidbody from detecting collisions between newly created coconuts and the player character's collider
Physics.IgnoreCollision(transform.root.collider, newCoconut.collider, true);
}
}
}