Skip to content

Instantly share code, notes, and snippets.

@KRNKRS
Last active July 20, 2017 15:09
Show Gist options
  • Save KRNKRS/d12054bc26a10e48203df2a7f08dcdb6 to your computer and use it in GitHub Desktop.
Save KRNKRS/d12054bc26a10e48203df2a7f08dcdb6 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