Skip to content

Instantly share code, notes, and snippets.

@AaronTrotter
Created January 31, 2019 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronTrotter/d85c2735de35919f9af6953fe1bd3826 to your computer and use it in GitHub Desktop.
Save AaronTrotter/d85c2735de35919f9af6953fe1bd3826 to your computer and use it in GitHub Desktop.
Unity Simple Follow Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleFollowScript : MonoBehaviour
{
public bool followPosition = true;
public bool followRotation = true;
public Transform objectToFollow;
public FollowDir lockFollow;
private Vector3 initPosition;
public int positionMultiplier = 1;
// Start is called before the first frame update
void Start()
{
initPosition = transform.position;
}
Vector3 tmpPos;
private void LateUpdate()
{
if (followPosition)
{
tmpPos.x = ((lockFollow.x) ? initPosition.x : objectToFollow.position.x) * positionMultiplier;
tmpPos.y = ((lockFollow.y) ? initPosition.y : objectToFollow.position.y) * positionMultiplier;
tmpPos.z = ((lockFollow.z) ? initPosition.z : objectToFollow.position.z) * positionMultiplier;
transform.position = tmpPos;
}
if (followRotation)
{
transform.rotation = objectToFollow.rotation;
}
}
[Serializable]
public class FollowDir
{
public bool x = true;
public bool y = true;
public bool z = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment