Advertisement
Guest User

PredictedProjectileVisual

a guest
Jul 5th, 2019
2,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 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.  
  14.     private Camera cam;
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         cam = Camera.main;
  20.         lineVisual.positionCount = lineSegment;
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void Update()
  25.     {
  26.         LaunchProjectile();
  27.     }
  28.  
  29.     void LaunchProjectile()
  30.     {
  31.         Ray camRay = cam.ScreenPointToRay(Input.mousePosition);
  32.         RaycastHit hit;
  33.  
  34.         if (Physics.Raycast(camRay, out hit, 100f, layer))
  35.         {
  36.             cursor.SetActive(true);
  37.             cursor.transform.position = hit.point + Vector3.up * 0.1f;
  38.  
  39.             Vector3 vo = CalculateVelocty(hit.point, shootPoint.position, 1f);
  40.  
  41.             Visualize(vo);
  42.  
  43.             transform.rotation = Quaternion.LookRotation(vo);
  44.  
  45.             if (Input.GetMouseButtonDown(0))
  46.             {
  47.                 Rigidbody obj = Instantiate(projectile, shootPoint.position, Quaternion.identity);
  48.                 obj.velocity = vo;
  49.             }
  50.         }
  51.     }
  52.  
  53.     void Visualize(Vector3 vo)
  54.     {
  55.         for (int i = 0; i < lineSegment; i++)
  56.         {
  57.             Vector3 pos = CalculatePosInTime(vo, i / (float)lineSegment);
  58.             lineVisual.SetPosition(i, pos);
  59.         }
  60.     }
  61.  
  62.     Vector3 CalculateVelocty(Vector3 target, Vector3 origin, float time)
  63.     {
  64.         Vector3 distance = target - origin;
  65.         Vector3 distanceXz = distance;
  66.         distanceXz.y = 0f;
  67.  
  68.         float sY = distance.y;
  69.         float sXz = distanceXz.magnitude;
  70.  
  71.         float Vxz = sXz * time;
  72.         float Vy = (sY / time) + (0.5f * Mathf.Abs(Physics.gravity.y) * time);
  73.  
  74.         Vector3 result = distanceXz.normalized;
  75.         result *= Vxz;
  76.         result.y = Vy;
  77.  
  78.         return result;
  79.     }
  80.  
  81.     Vector3 CalculatePosInTime(Vector3 vo, float time)
  82.     {
  83.         Vector3 Vxz = vo;
  84.         Vxz.y = 0f;
  85.  
  86.         Vector3 result = shootPoint.position + vo * time;
  87.         float sY = (-0.5f * Mathf.Abs(Physics.gravity.y) * (time * time)) + (vo.y * time) + shootPoint.position.y;
  88.  
  89.         result.y = sY;
  90.  
  91.         return result;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement