Skip to content

Instantly share code, notes, and snippets.

@terrynoya
Forked from KRNKRS/CameraOffset.cs
Created July 20, 2017 15:09
Show Gist options
  • Save terrynoya/81275c7d90e87d5e34bb877d5dc0303e to your computer and use it in GitHub Desktop.
Save terrynoya/81275c7d90e87d5e34bb877d5dc0303e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraOffset : MonoBehaviour
{
public float moveSpeed = 2;
private GameObject playerObj;
private Vector3 offset;
void Awake()
{
playerObj = GameObject.FindGameObjectWithTag("Player");
}
// Use this for initialization
void Start()
{
offset = this.transform.position - playerObj.transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
var nowPosition = this.transform.position;
var targetPosition = playerObj.transform.position + offset;
var newPos = Vector3.Lerp(nowPosition, targetPosition, moveSpeed * Time.deltaTime);
this.transform.position = newPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment