Search Unity

How to launch missiles linearly?

Discussion in 'Scripting' started by huseyinbaba58, Aug 3, 2020.

  1. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    Missile is launched by helicopter.
    But missile move randomly .
    I want to that missile and helicopter have to same alignment and missile have to move linearly.
     
  2. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    Set the missiles forward vector to that of the heli, then move it forward:

    on missile spawn:
    missile.transform.forward = helicopter.transform.forward;

    in your update loop:
    missile.transform.position += missile.transform.forward * speed;
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Don't forget to factor in Time.deltaTime on that update movement to keep it smooth on varied framerates.
     
  4. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    Good point! Also, this is just one of many, many ways to move an object; e.g. if you want to use physics/collision, you'll probably want to set velocity on the rigidbody in FixedUpdate instead of setting .position every frame.
     
  5. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    Thanks dude.I will try your advices.