Search Unity

Projectile with an Erratic flight path?

Discussion in 'Scripting' started by Sydious, Jul 29, 2014.

  1. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I would like to create a projectile that has an erratic flight path. Like when a rocket sort of corkscrews it's way to the target.

    I have NO idea how to go about this. Can someone push me down the correct path?

    I know how to instantiate a prefab and add force, but not sure how I would achieve an erratic corkscrew flight path to the target.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ooo interesting... googling around I found
    http://itween.pixelplacement.com/examples.php
    there is a section on "Generating Random Paths" (can't actual view the contents right now... )

    off the top of my head for a completely erratic you could pick a couple of waypoints part way between the start (x) and the target (y), lets call them (a) and (b). Then rather than heading from x to y direct the projectile does x a b y. Maybe even a random number of waypoints...

    corkscrewing... I'd look at having an "empty" gameobject go from x to y with the "projectile" child rotating around the empty (until distance to target is sufficiently small for the corkscrew to stop and the projectile go for the target)
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    A couple of different approaches.

    The first is not using physics, but pre-computing a path and then applying some form of distortion to it before having your projectile follow it.

    The second is to have a "desired location" that works as per usual, and have the projectile accelerate towards that location. If you start the projectile with a velocity and acceleration that does *not* point towards the initial desired location it will correct itself over time. Of course, this does mean you'll need to massage and tweak the values until you get them just right... Make it too agile and it'll eventually snap to the path anyway. Not agile enough and it won't get where you need it.
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    You could just setup the missile to be the child of a pivot point and add a simple spin animation or script to it. Then as the missile's pivot moves towards it's target the missile will corkscrew around until it hits.
     
  5. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I didn't think of using an animation. I will give that approach a shot. Thanks for your suggestions folks!
     
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
  7. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    Thanks novashot. That is exactly what i was looking for. Thank you for that. Your grenades also helped me work out an issue. Nice work and Thank You Very Much!
     
  8. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    No problem, just remember I had written those when i was first starting out and aren't the most efficient but are a good jump off point to get you going.