Created
April 16, 2022 08:03
-
-
Save Matthew-J-Spencer/887c10169e951b0b32e531e109d28a63 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 UnityEngine; | |
public class GunSprint : MonoBehaviour { | |
[SerializeField] private Bullet _bulletPrefab; | |
[SerializeField] private Transform _spawnPoint; | |
[SerializeField] private ParticleSystem _smokeSystem; | |
[SerializeField] private LayerMask _targetLayer; | |
[SerializeField] private float _bulletSpeed = 12; | |
[SerializeField] private float _torque = 120; | |
[SerializeField] private float _maxTorqueBonus = 150; | |
[SerializeField] private float _maxAngularVelocity = 10; | |
[SerializeField] private float _forceAmount = 600; | |
[SerializeField] private float _maxUpAssist = 30; | |
[SerializeField] private float _smokeLength = 0.5f; | |
[SerializeField] private float _maxY = 10; | |
private Rigidbody _rb; | |
private float _lastFired; | |
private bool _fire; | |
private void Awake() => _rb = GetComponent<Rigidbody>(); | |
void Update() { | |
// Clamp max velocity | |
_rb.angularVelocity = new Vector3(0, 0, Mathf.Clamp(_rb.angularVelocity.z, -_maxAngularVelocity, _maxAngularVelocity)); | |
if (Input.GetMouseButtonDown(0)) { | |
// Check if on target | |
var hitsTarget = Physics.Raycast(_spawnPoint.position, _spawnPoint.forward, float.PositiveInfinity, _targetLayer); | |
if (hitsTarget) GameManager.Instance.ToggleSlowMo(true); | |
// Spawn | |
var bullet = Instantiate(_bulletPrefab, _spawnPoint.position, _spawnPoint.rotation); | |
bullet.Init(_spawnPoint.forward * _bulletSpeed, hitsTarget); | |
_smokeSystem.Play(); | |
_lastFired = Time.time; | |
// Apply force - More up assist depending on y position | |
var assistPoint = Mathf.InverseLerp(0, _maxY, _rb.position.y); | |
var assistAmount = Mathf.Lerp(_maxUpAssist, 0, assistPoint); | |
var forceDir = -transform.forward * _forceAmount + Vector3.up * assistAmount; | |
if (_rb.position.y > _maxY) forceDir.y = Mathf.Min(0, forceDir.y); | |
_rb.AddForce(forceDir); | |
// Determine the additional torque to apply when swapping direction | |
var angularPoint = Mathf.InverseLerp(0, _maxAngularVelocity, Mathf.Abs(_rb.angularVelocity.z)); | |
var amount = Mathf.Lerp(0, _maxTorqueBonus, angularPoint); | |
var torque = _torque + amount; | |
// Apply torque | |
var dir = Vector3.Dot(_spawnPoint.forward, Vector3.right) < 0 ? Vector3.back : Vector3.forward; | |
_rb.AddTorque(dir * torque); | |
} | |
if (_smokeSystem.isPlaying && _lastFired + _smokeLength < Time.time) _smokeSystem.Stop(); | |
} | |
} | |
// Put this in its own file | |
public class Bullet : MonoBehaviour { | |
[SerializeField] private GameObject _explosionPrefab; | |
[SerializeField] private GameObject _trail; | |
private Rigidbody _rb; | |
private bool _hitsTarget; | |
private void Awake() => _rb = GetComponent<Rigidbody>(); | |
public void Init(Vector3 vel, bool hitsTarget) { | |
_rb.velocity = vel; | |
_hitsTarget = hitsTarget; | |
if (_hitsTarget) _trail.SetActive(true); | |
} | |
private void OnCollisionEnter(Collision collision) { | |
Instantiate(_explosionPrefab, transform.position, Quaternion.identity); | |
if (_hitsTarget) { | |
GameManager.Instance.ToggleSlowMo(false); | |
} | |
Destroy(gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I downloaded this to learn more about how this works, im new to unity so forgive me but what is GameManager? when i coped this code i get this error in visual studio "The name 'GameManager' does not exist in the current context [Assembly-CSharp]" How do i fix this? thanks