Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.