Search Unity

best method of achieving parabola-like movement of an object?

Discussion in 'Getting Started' started by TheOnlyTruth, Aug 29, 2016.

  1. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Hi. truth here :)

    // I am happy to say that i have moved a little bit from the start, and will soon be celebrating a whole month learning unity on daily basis!

    So i have a problem. I am making a tower defence game, and i want to add a cannon ball. What it should do is, when spawned, get the location of target. Then get "shot up" and then move towards target location in parabola-like manner.

    I can't get it to work. The first part works pretty okay-ish, im quite happy with it. It moves up and towards the target in nice manner.

    BUT, after it reaches the "first half" of parabola nad has to move DOWN, well... I dont know how to do it.

    ill paste code down bellow.

    //NOTE: you are more than welcome to post ideas for the first part of parabola-like movement, as well as my scripting in general too. I KNOW that my method of doing this is stupid. Note that its only few weeks im playing with unity OR any code in general.


    Code (CSharp):
    1. public class CannonBallScript : MonoBehaviour {
    2.    
    3.     public float speed = 10f;
    4.     public GameObject particleOnCol;
    5.  
    6.     public Vector3 testVector;
    7.  
    8.     public Transform target;
    9.     public Vector3 landingPos;
    10.     private float landingPosX;
    11.     private float landingPosZ;
    12.     private float startingPosY;
    13.     bool isInFirstStage = true;
    14.  
    15.     public void TrackTarget (Transform atarget) //grabs from turret.
    16.     {
    17.         target = atarget;
    18.     }
    19.  
    20.     void Start ()
    21.     {
    22.         landingPos = target.transform.position;
    23.         landingPosX = target.transform.position.x;
    24.         landingPosZ = target.transform.position.z;
    25.         startingPosY = transform.position.y;
    26.         Invoke ("ChangeStage", 0.5f); //end of first half of parabola.
    27.  
    28.     }
    29.  
    30.     void Update ()
    31.     {
    32.         if (isInFirstStage == true) {
    33.             GetPosStart ();
    34.         }
    35.         else
    36.         {
    37.             MoveToTarget ();
    38.         }
    39.     }
    40.  
    41.     void MoveToTarget () //falling part of the ball
    42.     {
    43.         transform.position = Vector3.MoveTowards (transform.position, landingPos, speed * Time.deltaTime);
    44.  
    45.         if (transform.position == landingPos)
    46.         {
    47.             print ("kaboom");
    48.             Destroy (gameObject);
    49.         }
    50.     }
    51.  
    52.     void GetPosStart () //ball is going up.
    53.     {
    54.         Vector3 GetHere = new Vector3 (landingPosX, startingPosY + 30f, landingPosZ);
    55.         transform.position = Vector3.MoveTowards (transform.position, GetHere, Time.deltaTime * speed);
    56.     }
    57.  
    58.     void ChangeStage ()
    59.     {
    60.         isInFirstStage = false;
    61.     }
    62. }
    63.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Almost this exact same question came up the other day, here. @jugnu never replied to let us know how it worked out, but I stand by my suggestion.

    And man, this must be a FAQ — in trying to dig that up, I found about a half-gazillion other people asking essentially the same thing. And about a quarter-gazillion got answers from me, which I don't like nearly as well as my latest answer. Perhaps I should write it up as a blog post.

    Anyway, please give that parabola equation a try, and let us know if it does the trick for you!
     
    Schneider21 likes this.
  3. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Thanks for the reply, Joe. You really should make a buy-me--a-cup-of-coffee donation link bellow in your signature.

    However, it seems that there are new things that i have never heard about before. Atan2, which i never heard of, and Quaternion, which still gives me total headaches. Im at work currently, i will surely dig deepr into the post you linked me to once im at home. I do feel, that i will have more questions, therefore, i will probably keep this post updated. :)
     
    Tset_Tsyung and JoeStrout like this.
  4. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Ehh, have to admit - math was not my favorite subject in school. I gaved up and just googled the code that i needed.

    However, Joe, i want to ask you something. It was a tad hard for me to understand everything you linked me to.

    What does the "k" stand for in the formula you provided? I might play with that a little >.<

    And, in the code i found, i wanted to make a few modifications and whenver i delete the first line (the yield return new) my script starts acting weird o_O it shoots the ball, but the ball never lands. And it alwasy seems to go to 0, inf, 0 position.


    Anyway, for those who have googled and ended up here... Here's a cheat code (providing it cause it uses Deg2Rad and sin/cos stuff... eww:



    Code (CSharp):
    1. IEnumerator CannonballMovement()
    2. {
    3.     // Short delay added before Projectile is thrown
    4.     yield return new WaitForSeconds(0f);
    5.  
    6.     // Calculate distance to target
    7.     float target_Distance = Vector3.Distance(transform.position, testVector);
    8.  
    9.     // Calculate the velocity needed to throw the object to the target at specified angle.
    10.     float projectile_Velocity = target_Distance / (Mathf.Sin(2 * privateRotation * Mathf.Deg2Rad) / dragDown);
    11.  
    12.     // Extract the X  Y componenent of the velocity
    13.     float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(privateRotation * Mathf.Deg2Rad);
    14.     float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(privateRotation * Mathf.Deg2Rad);
    15.  
    16.     // Calculate flight time.
    17.     float flightDuration = target_Distance / Vx;
    18.  
    19.     // Rotate projectile to face the target.
    20.     transform.rotation = Quaternion.LookRotation(testVector - transform.position);
    21.  
    22.     float elapse_time = 0;
    23.  
    24.     while (elapse_time < flightDuration)
    25.     {
    26.         transform.Translate (0, (Vy - (dragDown * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
    27.  
    28.         elapse_time += Time.deltaTime;
    29.  
    30.         yield return null;
    31.     }
    32. }
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    k stands for "constant," and in this case it just controls how much arc you have.

    And coroutines, yuck - I would not recommend them for a situation like this. But hey, if it works for you, that's great!

    Sounds like I really do need to write that blog post... Perhaps I can get to it later that week.
     
  6. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    @JoeStrout , if you were to make a blog post explaining why you do something and just throw in the logic/explanation behind each line... I can't say i would love you, cause i already do, but i think you would come a step closer to the day community builds a shrine for you :D
     
    dsoft, Tset_Tsyung and JoeStrout like this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Heh, well I don't think I need a shrine. A simple statue in the town square would suffice.

    I've put this at the top of my list of topics to blog about, and I'll get it done as soon as I get the chance.
     
    Tset_Tsyung and Kiwasi like this.
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  9. mafole4x

    mafole4x

    Joined:
    Dec 15, 2015
    Posts:
    6
    JoeStrout likes this.
  10. Armen138

    Armen138

    Joined:
    Oct 31, 2015
    Posts:
    1
  11. cagallo436

    cagallo436

    Joined:
    Oct 12, 2021
    Posts:
    6
  12. emredesu

    emredesu

    Joined:
    Aug 12, 2021
    Posts:
    55
    This blog post is great and it works very well.
    One question though: how would one make the object continue moving in a parabolic arc even after reaching the target?