how to make a camera follow an object in unity 2d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
//Object you want to follow
public Transform target;
//Set to z -10
public Vector3 offset;
//How much delay on the follow
[Range(1, 10)]
public float smoothing;
private void FixedUpdate()
{
Follow();
}
void Follow()
{
Vector3 targetPosition = target.position + offset;
Vector3 smoothPosition = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.fixedDeltaTime);
transform.position = smoothPosition;
}
}
4.5
2
public Transform player; public Vector3 offset; void Update () { transform.position = new Vector3 (player.position.x + offset.x, player.position.y + offset.y, offset.z); // Camera follows the player with specified offset position }
Thank you!
2
0
Are there any code examples left?
New code examples in category C#