Advertisement
AndrewRosyaev

2dBallisticExample

May 14th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Bullet : MonoBehaviour
  5. {
  6.     public int Speed;
  7.     public float Angle;
  8.     float t = Mathf.PI;
  9.  
  10.     Vector3 _StartPos;
  11.     Vector3 LastPos;
  12.  
  13.     // Use this for initialization
  14.     void Start ()
  15.     {
  16.         LastPos = transform.position;
  17.         Angle = Vector3.Angle (Vector3.forward, transform.forward);
  18.         Angle = Mathf.Deg2Rad * Angle;
  19.  
  20.         _StartPos = transform.position;
  21.     }
  22.  
  23.     void Update()
  24.     {
  25.         t += Time.deltaTime;
  26.         float vz = Speed*t* Mathf.Cos (Angle) * Time.deltaTime;
  27.         float vy = Speed*t* Mathf.Sin (Angle)* Time.deltaTime-(9.8f*(t*t))/2* Time.deltaTime;
  28.         Debug.Log (Mathf.Cos (Angle)*t);
  29.         Vector3 NextPos = new Vector3 (transform.position.x, transform.position.y + vy, transform.position.z + vz);
  30.         transform.position = NextPos;
  31.  
  32.         RaycastHit hit;
  33.         if (Physics.Linecast(LastPos, transform.position, out hit))
  34.         {
  35.             print (hit.transform.name);
  36.             Destroy (gameObject);
  37.         }
  38.         LastPos = transform.position;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement