Created
March 5, 2020 18:25
-
-
Save donfreiday/923ad796b4ec8489e2ad1df6412f7fce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Attach to camera to have it follow a GameObject in Unity 2d | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class FollowCharacter : MonoBehaviour | |
{ | |
public float interpolationSpeed = 0.5f; | |
public float verticalOffset = 4.0f; | |
public GameObject focusObject; | |
void Start() | |
{ | |
focusObject = GameObject.Find("Mario"); | |
} | |
void LateUpdate() | |
{ | |
Vector3 targetPosition = new Vector3( | |
focusObject.transform.position.x, | |
focusObject.transform.position.y + verticalOffset, | |
transform.position.z); | |
transform.position = Vector3.Lerp(transform.position, targetPosition, interpolationSpeed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment