Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Move an object towards a point

Discussion in 'Scripting' started by KTTdestroyer, Sep 11, 2014.

  1. KTTdestroyer

    KTTdestroyer

    Joined:
    Aug 31, 2014
    Posts:
    8
    To be more specific, i have a gun (starting point) and i have a bullet (rigidbody), i want the gun to fire the bullet towards a specific vector3(target), how do i do it??

    Probobly a silly question, but i have tried several ways and everytime the bullet either instantly moves to "target" or doesint move at all. Please help!
     
  2. KTTdestroyer

    KTTdestroyer

    Joined:
    Aug 31, 2014
    Posts:
    8
    Sorry for posting, i found a solution:

    Was:
    clone.velocity = transform.TransformDirection(Vector3.forward * 25);
    Is:
    clone.velocity = clone.transform.TransformDirection(Vector3.forward * 25);

    :)
     
  3. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Alternatively since you have a target you can try using Vector3.MoveTowards()
     
  4. KTTdestroyer

    KTTdestroyer

    Joined:
    Aug 31, 2014
    Posts:
    8
    Thanks! but what is the difference?
     
  5. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    If you want to have a true bullet behavior regardless of the target then your approach probably is a better one. If you want to have a kind of "guided missile" behavior when once target is determined and shot is performed then bullet needs to hit the target regardless of the target movement then my approach will help you. It all depends on what you need to achieve. You mentioned that you have to take a target position into account though I thought that I would mention an alternative.