Search Unity

how to increase speed of the projectile without affecting its velocity

Discussion in 'Physics' started by unity_iJdCMepZRpRVDw, Mar 6, 2019.

  1. unity_iJdCMepZRpRVDw

    unity_iJdCMepZRpRVDw

    Joined:
    Jan 4, 2019
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Projectile : MonoBehaviour
    6. {
    7.     // launch variables
    8.     [SerializeField] private Transform TargetObjectTF;
    9.     [Range(20.0f, 75.0f)] public float LaunchAngle;
    10.  
    11.     // state
    12.     private bool bTargetReady;
    13.     private bool bTouchingGround;
    14.  
    15.     // cache
    16.     private Rigidbody rigid;
    17.  
    18.     //private Vector3 initialPosition;
    19.     private Quaternion initialRotation;
    20.    
    21.     //-----------------------------------------------------------------------------------------------
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.     {  
    26.         rigid = GetComponent<Rigidbody>();
    27.         bTargetReady = false;
    28.         bTouchingGround = true;
    29.         initialRotation = transform.rotation;
    30.     }
    31.  
    32.     // resets the projectile to its initial position
    33.     void ResetToInitialState()
    34.     {
    35.         rigid.velocity = Vector3.zero;
    36.         bTouchingGround = true;
    37.         bTargetReady = false;
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update ()
    42.     {
    43.         if (Input.GetKeyDown(KeyCode.Space))
    44.         {
    45.                 Launch();
    46.         }
    47.  
    48.         if (!bTouchingGround && !bTargetReady)
    49.         {
    50.             // update the rotation of the projectile during trajectory motion
    51.             transform.rotation = Quaternion.LookRotation(rigid.velocity) * initialRotation;
    52.  
    53.         }
    54.     }
    55.  
    56.     void OnCollisionEnter()
    57.     {
    58.         bTouchingGround = true;
    59.     }
    60.  
    61.     void OnCollisionExit()
    62.     {
    63.         bTouchingGround = false;
    64.     }
    65.  
    66.     void Launch()
    67.     {
    68.         // think of it as top-down view of vectors:
    69.         //   we don't care about the y-component(height) of the initial and target position.
    70.         Vector3 projectileXZPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    71.         Vector3 targetXZPos = new Vector3(TargetObjectTF.position.x, transform.position.y, TargetObjectTF.position.z);
    72.  
    73.         // rotate the object to face the target
    74.         transform.LookAt(targetXZPos);
    75.  
    76.         // shorthands for the formula
    77.         float R = Vector3.Distance(projectileXZPos, targetXZPos) ;
    78.         float G = Physics.gravity.y;
    79.         float tanAlpha = Mathf.Tan(LaunchAngle * Mathf.Deg2Rad);
    80.         float H = (TargetObjectTF.position.y) - transform.position.y;
    81.  
    82.         // calculate the local space components of the velocity
    83.         // required to land the projectile on the target object
    84.         float Vz = Mathf.Sqrt(G * R * R / (2.0f * (H - R * tanAlpha))) ;
    85.         float Vy = tanAlpha * Vz ;
    86.  
    87.         // create the velocity vector in local space and get it in global space
    88.         Vector3 localVelocity = new Vector3(0f, Vy, Vz);
    89.         Vector3 globalVelocity = transform.TransformDirection(localVelocity);
    90.  
    91.         // launch the object by setting its initial velocity and flipping its state
    92.         rigid.velocity = globalVelocity;
    93.         bTargetReady = false;
    94.     }
    95. }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Your using ballistics, a greater speed means a different path, no way around it.

    How to get around it:
    1) increasing gravity will allow you to shoot faster at a lower angle, youll still encounter the problem, but at a different velocity/angle

    edit: actually a higher angle, my bad.

    2)save the path to a Vector3 array and lerp from one to the other, faking it
     
    Last edited: Mar 6, 2019
    ysshetty96 likes this.
  3. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Okay , can you tell how to store path in a vector3 array? And also while lerping on these stored points how to rotate the arrow? So that arrow should face upward at the start and while reaching enemy position it should point down. Can u sughsug me a proper way to do this pls?.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    A lot of ways to generate the array,
    You can manually put each position, but this will get tedious the bigger the array "resolution".
    My prefered way is making an editor(by that i mean a script that runs in the editor and not a part of the build, not a custom inspector) script that really shoots the projectile according to your tweaked parameters(this way you can "bake" it with a 2g gravity and play it in a normal 1g enviroment*) and saves its position every x time to a list and at the end export that as an array to where ever you want.

    Snapping the rotation is as simple as getting the direction of travel that you already have and snapping the transform.forward to it.
    Code (CSharp):
    1. transform.forward = ( positionArray[i-1] - positionArray[i] ).normlized;
    You could ofc "just" save the rotation along with the position with a "positionRotation" struct or a like but i think it's very unoptimal, thats info you can extrapolate from the positions.

    *You can have different object with different gravity values regardless of this and tge global gravity, lots of other ways.
     
    unity_iJdCMepZRpRVDw likes this.