Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    7
    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:
    20
    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:
    7
    Thank you, this really helped me out. <3