73 lines
1.6 KiB
C#
73 lines
1.6 KiB
C#
using UnityEngine;
|
|
|
|
public class TrajectoryThrow : MonoBehaviour
|
|
{
|
|
public LineRenderer lineRenderer;
|
|
|
|
public int pointsCount = 30;
|
|
|
|
public float initialVelocity = 15f;
|
|
|
|
public Transform launchPoint;
|
|
|
|
public Transform lookTarget;
|
|
|
|
public float timeStep = 0.1f;
|
|
|
|
public Vector3 gravity = Physics.gravity;
|
|
|
|
public bool lineRenderEnabled;
|
|
|
|
public bool targetCylinderEnabled;
|
|
|
|
public Transform targetCylinder;
|
|
|
|
public Vector3 cylinderPosition => targetCylinder.position;
|
|
|
|
private void Update()
|
|
{
|
|
DrawCylinderTarget();
|
|
if (lineRenderEnabled)
|
|
{
|
|
DrawLineTrajectory();
|
|
}
|
|
}
|
|
|
|
private void DrawCylinderTarget()
|
|
{
|
|
if (!targetCylinderEnabled)
|
|
{
|
|
targetCylinder.gameObject.SetActive(value: false);
|
|
return;
|
|
}
|
|
for (int i = 0; i < pointsCount; i++)
|
|
{
|
|
float num = (float)i * timeStep;
|
|
Vector3 vector = (lookTarget.position - launchPoint.position).normalized * initialVelocity;
|
|
Vector3 position = launchPoint.position;
|
|
Vector3 vector2 = vector * num + 1f * gravity * num;
|
|
Vector3 vector3 = position + vector2;
|
|
if ((vector3 + vector2).y <= 0f)
|
|
{
|
|
vector3.y = 0f;
|
|
targetCylinder.transform.position = vector3;
|
|
targetCylinder.transform.gameObject.SetActive(value: true);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DrawLineTrajectory()
|
|
{
|
|
lineRenderer.positionCount = pointsCount;
|
|
Vector3 position = launchPoint.position;
|
|
Vector3 vector = launchPoint.forward * initialVelocity;
|
|
for (int i = 0; i < pointsCount; i++)
|
|
{
|
|
float num = (float)i * timeStep;
|
|
Vector3 position2 = position + vector * num + 0.5f * gravity * num * num;
|
|
lineRenderer.SetPosition(i, position2);
|
|
}
|
|
}
|
|
}
|