Created
November 23, 2021 15:47
-
-
Save nbogie/0bc1ce54019fda95f779fcc338e7338b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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