Created
June 3, 2020 15:46
-
-
Save SolidAlloy/b428f6d5ec00d2dfff29972453c55d00 to your computer and use it in GitHub Desktop.
More useful version of the AddPhysicalImpact script from the fBasic Asset.
This file contains 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; | |
using UnityEngine.Events; | |
using Random = UnityEngine.Random; | |
namespace FIMSpace.Basics | |
{ | |
/// <summary> | |
/// Simple class which is adding physical impact on target rigidbody for provided time | |
/// </summary> | |
public class FBasic_AddPhysicalImpact : MonoBehaviour | |
{ | |
[SerializeField] private Rigidbody _targetRigidbody; | |
[SerializeField] private Vector3 _pushDirection = Vector3.forward; | |
[SerializeField] private float _pushDirectionSeconds = 1f; | |
[SerializeField] private Vector3 _randomDirMultiplier = Vector3.zero; | |
[SerializeField] private float _powerMultiplier = 10f; | |
[SerializeField] private UnityEvent OnPhysicalImpactDisable; | |
private Vector3 _randomImpact = Vector3.zero; | |
private float _timer; | |
private void Reset() | |
{ | |
_targetRigidbody = GetComponentInChildren<Rigidbody>(); | |
} | |
private void OnEnable() | |
{ | |
if (_targetRigidbody == null) | |
{ | |
enabled = false; | |
return; | |
} | |
if (_randomDirMultiplier.x != 0f) _randomImpact.x = Random.Range(0f, _randomDirMultiplier.x); | |
if (_randomDirMultiplier.y != 0f) _randomImpact.y = Random.Range(0f, _randomDirMultiplier.y); | |
if (_randomDirMultiplier.z != 0f) _randomImpact.z = Random.Range(0f, _randomDirMultiplier.z); | |
_randomImpact.Normalize(); | |
var sum = _randomImpact.x + _randomImpact.y + _randomImpact.z; | |
if (sum != 0f) _randomImpact /= sum; | |
} | |
private void OnDisable() | |
{ | |
OnPhysicalImpactDisable?.Invoke(); | |
} | |
private void Update() | |
{ | |
_targetRigidbody.AddForce((_pushDirection + _randomImpact) * _powerMultiplier, ForceMode.Force); | |
if (_timer > _pushDirectionSeconds) | |
{ | |
enabled = false; | |
_timer = 0f; | |
} | |
_timer += Time.deltaTime; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment