Created
January 31, 2019 17:39
-
-
Save AaronTrotter/d85c2735de35919f9af6953fe1bd3826 to your computer and use it in GitHub Desktop.
Unity Simple Follow Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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