Search Unity

Projectile with controlled velocity and angle (Like in Worms)

Discussion in '2D' started by ddripper, Oct 23, 2016.

  1. ddripper

    ddripper

    Joined:
    Sep 19, 2016
    Posts:
    3
    Hello there. I'm a beginner in Unity and I'm stuck with this particular issue (I couldn't find the answer to this, so I'm posting this here)
    So I'm trying to make a 2D turn based 'snowball fight' game where the 2 players gets turns to throw snowballs at each other.
    As the title suggests, I am having a hard time figuring out a script which allows the player to throw a projectile with controlled velocity at a particular angle, just like in Worms Armageddon, where the longer you hold the key, the greater is the velocity of the projectile.
    I would really be grateful if you could help me with this.
    Thank you for your time.
     
  2. kozle

    kozle

    Joined:
    Aug 6, 2015
    Posts:
    46
    You can calculate angle from player coordinates and mouse click coordinates. Take a look at http://www.vitutor.com/geometry/vec/angle_vectors.html

    For calculating force of a throw, you need to get the time difference of mouseDOWN and mouseUP, then apply that amount of force to the object.
     
    ddripper likes this.
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Atan or atan2 should give you the angle I believe also, kozle idea for force sounds a good one to me but I would also clamp it so it can't get to higher value.