Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Make a Gameobject follow another one with adjustable distance and target to follow?

Discussion in 'Scripting' started by MirioTogata, Oct 4, 2018.

  1. MirioTogata

    MirioTogata

    Joined:
    Oct 4, 2018
    Posts:
    5
    Hi,
    I want to make a script where a Gameobject follows another one and want to be able to adjust the selected target that should be followed and the distance between them in the inspector.
    I'm hoping to get some help here since I couldn't find anything about this :(
    Thanks, Mirio
     
  2. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    17
    There is no specific function for that so you didn't find anything but you can do it very easy!

    Basically what you want to do is to update one object position depending on other object position and you want some offset so both objects are not exactly in the same spot so they don't collapse.

    Attach script to object that you want to be a follower. Refer object to follow in inspector and play with offset variable to adjust how far objects are from each other. You can modify offset variable in scripts later on to make them move closer to each other for example
    Code (CSharp):
    1. public Transform objectToFollow;
    2. public Vector3 offset;
    3. void Update(){
    4.    transform.position = objectToFollow.position + offset;
    5. }
    Good luck!
     
  3. MirioTogata

    MirioTogata

    Joined:
    Oct 4, 2018
    Posts:
    5
    Thank you, this really helped me out. <3