Search Unity

make object follow other without parenting

Discussion in 'Scripting' started by freezingwreck, Sep 19, 2013.

  1. freezingwreck

    freezingwreck

    Joined:
    Mar 24, 2013
    Posts:
    23
    Lets see how should i say this... I have Object1 and Object2 and I want Object2 to follow Object1 but not on the z- axis. I dont want to accomplish this by making Object2 a child of Object2. Im still learning how to code. I need to somehow have a script that reads the x and y location of Object1 and make Object2 go there. I think this will require 2 scripts but im still trying to wrap my head around calling another script inside a script.

    oh and im much more familiar with javascript than c# so if i could get some help in javascript that would be preferable.
     
    Last edited: Sep 19, 2013
  2. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    294
    am not familiar with javascript, but the script should be something like this (if added to the one that follows):
    in awake()
    other = GameObject.find("followthis");

    in update()
    transform.position = other.transform.position;


    if you want to ignore z .. you can try this

    transform.position.x = other.transform.position.x;
    transform.position.y = other.transform.position.y;
     
    Last edited: Sep 19, 2013
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
unityunity