Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Getting an object to travel towards another

Discussion in 'Scripting' started by rmlloyd, Aug 20, 2008.

  1. rmlloyd

    rmlloyd

    Joined:
    Jul 22, 2008
    Posts:
    82
    Hi,
    hope someone can help with this. Currently making my first game in Unity and getting stuck along the way. I have tried to find answers to this among the script help but to no avail.

    I have a ship which fires lasers towards the mouse pointer. At the moment I have the laser instantiating and also pointing to where the mouse is/was at the time it was instantiated.

    Code (csharp):
    1.  
    2. var weapon_1 = Instantiate(weapon_1, transform.position, transform.rotation);  
    3.                
    4. //Rotate the laser to face the mouse pointer object
    5. weapon_1.transform.LookAt(target);
    6.  
    However, I am stuck on the next part, making the missile move towards the mouse pointer (at the point it was clicked). Are there commands in Unity to move an object towards another? Or would it be the case of working out some complex calculation to get the correct amount to move along transform.position.x/y/z?

    Thanks a lot for your help.

    [/code]
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You have your starting position (where it's instantiated) and your target position (the point of clicking), you can then either:

    (a) Use a laser/projectile/whatever speed and move along the line of travel (line of travel = target position - starting position)

    (b) Create a starting position transform and a target position transform and then linearly interpolate between the two (lookup Lerp in the docs)

    I would guess that your best bet is something like option (a), and then translate the projectile/laser/whatever's transform along the path using the speed you specify.
     
  3. rmlloyd

    rmlloyd

    Joined:
    Jul 22, 2008
    Posts:
    82
    cool,

    thanks a lot. Option A sounds like the best bet, although not initially simple to setup.

    thanks for your help.