Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Firing Projectile in Arc in 2D

Discussion in 'Scripting' started by Matt-K, May 26, 2019.

  1. Matt-K

    Matt-K

    Joined:
    Sep 10, 2018
    Posts:
    79
    Hey,

    I have been messing around with trying to launch a projectile over a target. I couldn't figure out how to AddForce at the desired angle which led me to math and code I don't understand. X.X

    I can AddForce Vector2.up and AddForce Vector2.right but I cannot figure out how to do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,469
    Matt-K likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What I do with rigidbody ballistic projectiles is this:

    1) calculate the firing angle, rotate the 'Turret' gameobject to said rotation.
    2) Instantiate the projectile gameobject on the tip of the barrel, I have a particle system on the end called "muzzle" so i use that, you could also do "turret.position + (turret.forward * turret.barrelLength)". (ofcource with the same rotation as the turret)
    3) just add local force in positive Z to the projectile, add the same amount of force as you used for the calculation.

    this link is also helpful: https://en.wikipedia.org/wiki/Projectile_motion

    I can give you a hand but in 3D if it applies, how are you approaching it?, you've just said "I'm messing with it".. - how?
     
    Matt-K likes this.
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    You can use my solution here:

    https://stackoverflow.com/questions...th-initial-impulse-in-unity/42794800#42794800
     
    Matt-K likes this.
  5. Matt-K

    Matt-K

    Joined:
    Sep 10, 2018
    Posts:
    79
    Thanks for responding! This is all really great! I need an education in Classical Mechanics among other things lol. I really appreciate everyone's feedback!
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I think @Kurt-Dekker's response was far too complicated and I think perhaps the OP needs to be a bit more specific with their question, if you're talking about a simple arc you could make an arc called a parabola arc and this will do the job in most cases.



    Are you by any chance looking for something more complicated though? Like being able to make a parabolic arc that calculates for you where a rigidbody might hit?
     
    Kurt-Dekker likes this.