Advertisement
romi_fauzi

PredictedProjectileVisual-FullLength

Jul 5th, 2020 (edited)
5,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Projectile : MonoBehaviour
  6. {
  7.     public Rigidbody projectile;
  8.     public GameObject cursor;
  9.     public Transform shootPoint;
  10.     public LayerMask layer;
  11.     public LineRenderer lineVisual;
  12.     public int lineSegment = 10;
  13.     public float flightTime = 1f;
  14.  
  15.     private Camera cam;
  16.  
  17.     // Start is called before the first frame update
  18.     void Start()
  19.     {
  20.         cam = Camera.main;
  21.         lineVisual.positionCount = lineSegment + 1;
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     void Update()
  26.     {
  27.         LaunchProjectile();
  28.     }
  29.  
  30.     void LaunchProjectile()
  31.     {
  32.         Ray camRay = cam.ScreenPointToRay(Input.mousePosition);
  33.         RaycastHit hit;
  34.  
  35.         if (Physics.Raycast(camRay, out hit, 100f, layer))
  36.         {
  37.             cursor.SetActive(true);
  38.             cursor.transform.position = hit.point + Vector3.up * 0.1f;
  39.  
  40.             Vector3 vo = CalculateVelocty(hit.point, shootPoint.position, flightTime);
  41.  
  42.             Visualize(vo, cursor.transform.position); //we include the cursor position as the final nodes for the line visual position
  43.  
  44.             transform.rotation = Quaternion.LookRotation(vo);
  45.  
  46.             if (Input.GetMouseButtonDown(0))
  47.             {
  48.                 Rigidbody obj = Instantiate(projectile, shootPoint.position, Quaternion.identity);
  49.                 obj.velocity = vo;
  50.             }
  51.         }
  52.     }
  53.  
  54.     //added final position argument to draw the last line node to the actual target
  55.     void Visualize(Vector3 vo, Vector3 finalPos)
  56.     {
  57.         for (int i = 0; i < lineSegment; i++)
  58.         {
  59.             Vector3 pos = CalculatePosInTime(vo, (i / (float)lineSegment) * flightTime);
  60.             lineVisual.SetPosition(i, pos);
  61.         }
  62.  
  63.         lineVisual.SetPosition(lineSegment, finalPos);
  64.     }
  65.  
  66.     Vector3 CalculateVelocty(Vector3 target, Vector3 origin, float time)
  67.     {
  68.         Vector3 distance = target - origin;
  69.         Vector3 distanceXz = distance;
  70.         distanceXz.y = 0f;
  71.  
  72.         float sY = distance.y;
  73.         float sXz = distanceXz.magnitude;
  74.  
  75.         float Vxz = sXz / time;
  76.         float Vy = (sY / time) + (0.5f * Mathf.Abs(Physics.gravity.y) * time);
  77.  
  78.         Vector3 result = distanceXz.normalized;
  79.         result *= Vxz;
  80.         result.y = Vy;
  81.  
  82.         return result;
  83.     }
  84.  
  85.     Vector3 CalculatePosInTime(Vector3 vo, float time)
  86.     {
  87.         Vector3 Vxz = vo;
  88.         Vxz.y = 0f;
  89.  
  90.         Vector3 result = shootPoint.position + vo * time;
  91.         float sY = (-0.5f * Mathf.Abs(Physics.gravity.y) * (time * time)) + (vo.y * time) + shootPoint.position.y;
  92.  
  93.         result.y = sY;
  94.  
  95.         return result;
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement