Skip to content

Instantly share code, notes, and snippets.

@nbogie
Created November 23, 2021 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbogie/0bc1ce54019fda95f779fcc338e7338b to your computer and use it in GitHub Desktop.
Save nbogie/0bc1ce54019fda95f779fcc338e7338b to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileFirer : MonoBehaviour
{
public List<GameObject> projectilePrefabs;
public float fireStrength = 20;
public float spinStrength = 10f;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
Fire();
}
}
void Fire()
{
GameObject clone = Instantiate(Pick(projectilePrefabs), transform.position+transform.forward*2f, transform.rotation);
Rigidbody rb = clone.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * fireStrength, ForceMode.Impulse);
rb.AddTorque(Random.insideUnitSphere * spinStrength);
}
T Pick<T>(List<T> arr)
{
return arr[Random.Range(0, arr.Count)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment