LSBUGPG / UnityLibrary

A library of useful Unity components and test scenes
GNU General Public License v3.0
0 stars 0 forks source link

launching first-person projectiles #42

Open notleeandrew opened 8 years ago

notleeandrew commented 8 years ago

i want to be able to fire projectiles

JammieDodgers commented 8 years ago

So, for this to work, you'll need to create a prefab to instantiate as a projectile. I've called mine projectile, but the beauty of programming is you can name it "ceiling cat is watching you masturbate", if you want to really confuse hackers.

using UnityEngine; using System.Collections;

public class Shooting : MonoBehaviour {

GameObject prefab;

void Start () {
    prefab = Resources.Load ("projectile") as GameObject;
}

void Update () {
    if (Input.GetMouseButtonDown(0)) {
        GameObject projectile = Instantiate (prefab) as GameObject;
        projectile.transform.position = transform.position + Camera.main.transform.forward *2;
        Rigidbody rb = projectile.GetComponent<Rigidbody> ();
        rb.velocity = Camera.main.transform.forward * 40;
    }
}

}

charliedadams commented 7 years ago

this code works. it is easy to understand. wish i don't have a wish for this.