Does anyone know the script for a smooth camera follow of the main game object?
I've been researching this for about two weeks and have come across nothing that works. Anything will be helpful!
Answer by Lanthuas · Nov 20, 2018 at 08:28 AM
This works really nice for me. You can also find it on official unity documentation page.
// Smooth towards the target
using UnityEngine;
using System.Collections;
public class DampCamera2D : MonoBehaviour
{
public Transform target;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
void Update()
{
// Define a target position above and behind the target transform
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));
// Smoothly move the camera towards that target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}
This doesn't work at all for me the character just runs away from the camera, though the camera does mirror the player rotation.
@ShamusO You just need to change this line of code: Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10)); Change the 5 to a 0-2. This line of code deter$$anonymous$$es how far the camera should be at all times from the target. Here the camera is always 5 points above the target at all times.
Answer by rocker182 · Jun 18, 2019 at 08:52 PM
I usually go with something i can tweak in editor i set the right position in editor and voilla this helps lerp camera in place nicely:
public Transform target;
public Vector3 target_Offset;
private void Start()
{
target_Offset = transform.position - target.position;
}
void Update()
{
if (target)
{
transform.position = Vector3.Lerp(transform.position, target.position+target_Offset, 0.1f);
}
}`
For some reason, $$anonymous$$y camera shakes a bit with this sort of setup. Like it is stuttering when the object gets to its "$$anonymous$$ax distance". Do you know why that is?
you can find smooth camera follow script here:-
https://gamedevsolutions.com/?p=79
hope it was helpful.
Answer by Zoogyburger · Mar 12, 2016 at 07:47 PM
Here's a real simple follow camera script:
public Transform target;
void Update()
{
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f);
}
}
}
Just put the script on the Camera and in the Inspector set the target to be the main gameobject.
Unfortunately, that didn't work. I tried different things with it and it didn't work.
How did it not work? $$anonymous$$ake sure the Target is Set to be the Player.
$$anonymous$$aybe this will work:
public Transform target;
void Update()
{
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
}
}
}
Answer by hexagonius · Mar 12, 2016 at 04:27 PM
install the standard assets with Unity. there's a follow script packed along with it
Your answer
Follow this Question
Related Questions
I build a own game but i have a cam problem 0 Answers
Camera Roll/Headbob? 0 Answers
Camera follow issue. 2 Answers
Camera EdgeScrolling Question 0 Answers