-
-
Save nomi404/44085682227695e4ff8b84f87b477484 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; | |
using UnityEngine.SceneManagement; | |
public class controller : MonoBehaviour | |
{ | |
[SerializeField] private bullet _bulletPrefab; | |
[SerializeField] private Transform _spawnPoint; | |
[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; | |
/// Simple smoke effects and flash prefab and audio. | |
[SerializeField] GameObject muzzleFlash; | |
[SerializeField] GameObject smokeEffect; | |
[SerializeField] Transform flashPos; | |
AudioSource mAudioSource; | |
[SerializeField] private float _maxY = 10; | |
private Rigidbody _rb; | |
private void Awake() | |
{ | |
_rb = GetComponent<Rigidbody>(); | |
mAudioSource = GetComponent<AudioSource>(); | |
} | |
void Update() | |
{ | |
// Clamp max velocity | |
_rb.angularVelocity = new Vector3(0, 0, Mathf.Clamp(_rb.angularVelocity.z, -_maxAngularVelocity, _maxAngularVelocity)); | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
// Spawn | |
var bullet = Instantiate(_bulletPrefab, _spawnPoint.position, _spawnPoint.rotation); | |
bullet.Init(_spawnPoint.forward * _bulletSpeed); | |
var flash=Instantiate(muzzleFlash,flashPos.position,Quaternion.identity); | |
var smoke=Instantiate(smokeEffect, flashPos.position,Quaternion.identity); | |
smoke.transform.parent=gameObject.transform; | |
Destroy(flash, 1f); | |
mAudioSource.Play(); | |
// 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment