Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowToPoint : MonoBehaviour
{
public Rigidbody beingThrown;
private Rigidbody throwable;
public GameObject moveSphere;
public GameObject positionForThrow;
public Vector3 target;
RaycastHit targethit;
private float h;
public float heightOverTarget = 5f;
public float gravity = -9.81f;
public bool drawLine;
//MOUSE MOVE
public float horizontalSpeed;
public float verticalSpeed;
public float radius = 5;
//CameraMove
public GameObject Cam;
//
public LayerMask IgnorLayer;
private LineRenderer LR;
void Start()
{
LR = GetComponent<LineRenderer>();
}
void Update()
{
//AimWithCamera();
UpdateHeight();
if (Input.GetMouseButton(1))
{
drawLine = true;
LR.enabled = true;
MoveTarget();
}
else
{
drawLine = false;
LR.enabled = false;
}
if (Input.GetMouseButtonDown(0))
{
throwable = Instantiate(beingThrown, positionForThrow.transform.position, transform.rotation) as Rigidbody;
Physics.IgnoreCollision(throwable.GetComponent<Collider>(), GetComponent<Collider>());
Launch();
}
if (drawLine)
{
DrawPath();
}
}
void Launch()
{
Physics.gravity = Vector3.up * gravity;
throwable.useGravity = true;
throwable.velocity = CalculateThrowData().initialVelocity;
}
ThrowData CalculateThrowData()
{
float displacementY = target.y - positionForThrow.transform.position.y;
Vector3 displacementXZ = new Vector3(target.x - positionForThrow.transform.position.x, 0, target.z - positionForThrow.transform.position.z);
float time = Mathf.Sqrt(-2 * h / gravity) + Mathf.Sqrt(2 * (displacementY - h) / gravity);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * h);
Vector3 velocityXZ = displacementXZ / time;
return new ThrowData(velocityXZ + velocityY * -Mathf.Sign(gravity), time);
}
void DrawPath()
{
if(throwable != null) {
ThrowData launchData = CalculateThrowData();
Vector3 previousDrawPoint = positionForThrow.transform.position;
int resolution = 30;
for (int i = 1; i <= resolution; i++)
{
float simulationTime = i / (float)resolution * launchData.timeToTarget;
Vector3 displacement = launchData.initialVelocity * simulationTime + Vector3.up * gravity * simulationTime * simulationTime / 2f;
Vector3 drawPoint = positionForThrow.transform.position + displacement;
Debug.DrawLine(previousDrawPoint, drawPoint, Color.green);
previousDrawPoint = drawPoint;
LR.SetPosition(i-1, drawPoint);
}
}
}
struct ThrowData
{
public readonly Vector3 initialVelocity;
public readonly float timeToTarget;
public ThrowData(Vector3 initialVelocity, float timeToTarget)
{
this.initialVelocity = initialVelocity;
this.timeToTarget = timeToTarget;
}
}
void MoveTarget()
{
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
moveSphere.transform.Translate(-v, 0, h);
Vector3 centerPosition = transform.localPosition;
float distance = Vector3.Distance(moveSphere.transform.position, centerPosition);
if (distance > radius)
{
Debug.Log("bigger");
Vector3 fromOriginToObject = moveSphere.transform.position - centerPosition;
fromOriginToObject *= radius / distance;
moveSphere.transform.position = centerPosition + fromOriginToObject;
}
if (Physics.Raycast(moveSphere.transform.position + new Vector3(0, 10, 0), Vector3.down, out targethit))
{
target = targethit.point;
}
}
void AimWithCamera()
{
RaycastHit hit;
if (Physics.Linecast(Cam.transform.position , Cam.transform.position + Cam.transform.forward * 20, out hit, IgnorLayer))
{
Debug.Log("HIT");
Debug.Log(hit.transform.name);
target = hit.point;
}
else { Debug.Log("Dont"); }
}
void UpdateHeight()
{
if (positionForThrow.transform.position.y > target.y)
{ h = positionForThrow.transform.position.y - target.y + heightOverTarget; }
if (target.y > positionForThrow.transform.position.y)
{ h = target.y - positionForThrow.transform.position.y + heightOverTarget; }
}
public void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(target, 1f);
Gizmos.DrawLine(Cam.transform.position, Cam.transform.forward);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment