Camera follow scripts not working?
I am making a 2d game in which the player is going downward at a certain velocity that is determined by the user's input. Hitting w makes velocity increase, s makes it decrease. I had the camera as a child of the player prefab to make it follow the player perfectly and that worked except I think it caused some issues with choppy movement. (with interpolation on too). So now I have been trying to instead use a script to accomplish this without making the camera a child of the player.
Every script I have tried has ended up with the same result, where the player just drops off the screen then comes in from the top and drops down again, repeatedly. See this video for what I mean: Camera follow
Anyone have any ideas what the issue might be? Much appreciated.
Answer by Zoogyburger · Feb 15, 2016 at 04:16 AM
This is my Camera script:
using UnityEngine;
using System.Collections;
public class FollowCamera : MonoBehaviour {
public Transform target;
Camera mycam;
void Start()
{
mycam = GetComponent<Camera> ();
}
void Update()
{
if (target)
{
transform.position = Vector3.Lerp (transform.position, target.position, 0.1f) + new Vector3 (0, 0, -10);
}
}
}
All you have to do is add this script to your camera, set the Target to be your player(in the Inspector) and of course make sure it isn't a child of the player anymore.
That's very helpful. Actually follows the player more or less the way it is supposed to. But could you explain what the x,y, and z coordinates of the new Vector3 you add to Vector3.lerp do?
In my game I transport the player from place to place but unless I have the camera set to 0, 0, -10 the camera will zoom in way up to the map and you won't be able to see anything. Because in 2d games you have to make sure the camera is always at -10. Glad I could help you! If you need anymore help tell me:)