Created
August 15, 2024 13:25
-
-
Save hman278/c06f4267d165342c6b70230cf3cbd2aa to your computer and use it in GitHub Desktop.
Opsive UltimateCharacterController Tracer
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 Cysharp.Threading.Tasks; | |
using Opsive.Shared.Game; | |
using UnityEngine; | |
/// <summary> | |
/// This custom tracer will show a Trail Renderer that will travel from | |
/// start point to end point. | |
/// </summary> | |
public class Tracer : MonoBehaviour | |
{ | |
[Tooltip("The length of the bullet.")] | |
[SerializeField] private float _bulletLength = 1f; | |
[Tooltip("The speed at which the bullet will fly in the shot direction initially.")] | |
[SerializeField] private float _initialSpeed = 20f; | |
[Tooltip("Max bullet speed over time.")] | |
[SerializeField] private float _maxSpeed = 200f; | |
[Tooltip("This variable represents the maximum time the trail should be visible after arriving at its destination.")] | |
[SerializeField] private float _maxVisibleTimeAfterHit = 0.05f; | |
private LineRenderer _lineRenderer; | |
private Vector3 _startPoint; | |
private Vector3 _endPoint; | |
/// <summary> | |
/// Initialize the default values. | |
/// </summary> | |
private void Awake() | |
{ | |
_lineRenderer = GetComponent<LineRenderer>(); | |
} | |
public void Initialize(Vector3 hitPoint) | |
{ | |
_startPoint = transform.position; | |
_endPoint = hitPoint; | |
Debug.Log(_startPoint); | |
MoveLineRendererPoints().Forget(); | |
} | |
/// <summary> | |
/// Coroutine to handle LineRenderer point movement. | |
/// </summary> | |
private async UniTaskVoid MoveLineRendererPoints() | |
{ | |
float travelLengthA = (_startPoint - _endPoint).magnitude; | |
float travelTime = travelLengthA / _initialSpeed; | |
float elapsedTime = 0.0f; | |
Vector3 bulletDirection =(_endPoint - _startPoint).normalized; | |
float speed = _initialSpeed; | |
while(elapsedTime < travelTime) | |
{ | |
elapsedTime += Time.deltaTime; | |
float fractionOfTraveledDistance = elapsedTime / travelTime; | |
float currentSpeed = Mathf.Lerp(_initialSpeed, _maxSpeed, fractionOfTraveledDistance); | |
travelTime = travelLengthA / currentSpeed; | |
// Back point | |
Vector3 posA = Vector3.Lerp(_startPoint, _endPoint, Mathf.Clamp01(fractionOfTraveledDistance)); | |
// Front point | |
Vector3 posB = posA + bulletDirection * _bulletLength; // Vector3.Lerp(_startPoint + bulletDirection * _bulletLength, _endPoint, Mathf.Clamp01(fractionOfTraveledDistance)); | |
_lineRenderer.SetPosition(0, posA); | |
_lineRenderer.SetPosition(1, posB); | |
await UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate); | |
} | |
_lineRenderer.SetPosition(1, _endPoint); | |
float waitTime = Mathf.Min(_maxVisibleTimeAfterHit, travelLengthA / _initialSpeed); | |
await UniTask.WaitForSeconds(waitTime); | |
DestroyObject(); | |
} | |
/// <summary> | |
/// Places the object back in the object pool. | |
/// </summary> | |
private void DestroyObject() | |
{ | |
ObjectPoolBase.Destroy(gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment