Helpers.FollowObject - robblofield/TomeboundDocs GitHub Wiki

Helpers.FollowObject

Author: Rob Blofield
Last Updated: 18/11/2023

Overview

A modular script to allow objects to follow other game objects via transform.position and transform.rotation without parenting and limited to any chosen axis.

Dependencies

N/A

Contents

  • Use case
  • Breakdown of code
    • Variables
    • Update()
    • LateUpdate()
    • SetPosition()
    • SetRotation()
  • Future Expansion
  • Full Code Reference

Use case

This is a modular script, it is designed to be used in many different cases where you need an object in the scene to follow the movement of another object, or individual values of the target object (x/y/z position and rotation. It is planned to expand this script to include more useful modular tools. see the future expansion section below.

Breakdown of Code

Variables

public GameObject followTarget;
private Vector3 destination;
private Quaternion destRotation;

public bool followPosX;
public bool followPosY;
public bool followPosZ;
public bool followRotX;
public bool followRotY;
public bool followRotZ;

GameObject followTarget: The object in the scene we want to follow (pos/rot/both).

Vector3 destination: A Vector3 to hold the calculated x/y/z position we are moving to. This script allows the following of individual values whilst maintaining existing values. They are combined and stored in this variable ready for movement.

Quaternion destRotation: As above with the destination, destRotation stores the desired x/y/z quaternion for our destination.

bool followPosX/Y/Z: Booleans for setting whether we want to follow X/Y/Z position of the target object.

bool followRotX/Y/Z: Booleans for setting whether we want to follow X/Y/Z rotation of the target object.

bool lockRotation: A boolean that locks rotation

Update()

    void Update()
    {
        destination = transform.position;
        destRotation = transform.rotation;
    }

Sets the current position and rotation before moving to new target

LateUpdate()

private void LateUpdate()
{
    SetPosition();
    SetRotation();
}

Run the SetPosition() and SetRotation() method. They are called in LateUpdate so that the position is calculated after objects are moved in update. This should work in the majority of cases, unless the target itself is moved in LateUpdate().

SetPosition()

A method to handle the calculation and movement to the desired position. The values are calculated between the object's original values and the values of the target object as directed by the bool variables.

public void SetPosition()
{
    if (followPosX)
    {
        destination.x = (followTarget.transform.position.x);
    }
    if (followPosY)
    {
        destination.y = (followTarget.transform.position.y);
    }
    if (followPosZ)
    {
        destination.z = (followTarget.transform.position.z);
    }
    transform.position = destination;

}

The value for destination.x/y/z are set depending on whether the booleans for "followPos.x/y/z" are enabled in the inspector.

SetRotation()

A method to handle the calculation and transition to the desired rotation. The values are calculated between the object's original values and the values of the target object as directed by the bool variables.

 public void SetRotation()
 {
     if (!lockRotation)
     {
         if (followRotX)
         {
             destRotation.x = (followTarget.transform.rotation.x);
         }
         if (followRotY)
         {
             destRotation.y = (followTarget.transform.rotation.y);
         }
         if (followRotZ)
         {
             destRotation.z = (followTarget.transform.rotation.z);
         }
         transform.rotation = destRotation;
     }
     else
     {
         transform.rotation = Quaternion.identity;
     }

The value for destRotation.x/y/z are set depending on whether the booleans for "followRot.x/y/z" are enabled in the inspector.

Future Expansion

This script is intended to be expanded upon as a general purpose follow object tool. Additional functionality that will be added to this script include:

  • Object offset - A set distance in x/y/x space from the original object
  • Invert Movement (x/y/z) - an option to invert/mirror movements in a given axis