- Home /
Camera follow script bug (C# & 2D)
I am using the following camera follow script and whenever my character (player) gets to about -15 on the x-axis everything on the camera disappears and I am unable to see the game objects. Please help! Thank you!
using UnityEngine; using System.Collections;
public class CameraFollow : MonoBehaviour {
[SerializeField] Transform character;
private Vector3 moveTemp;
[SerializeField] float speed = 3;
[SerializeField] float xDifference;
[SerializeField] float yDifference;
[SerializeField] float zDifference;
[SerializeField] float movementThreshold = 3;
void Start () {
}
void Update ()
{
if (character.transform.position.x > transform.position.x)
{
xDifference = character.transform.position.x - transform.position.x;
}
else
{
xDifference = transform.position.x - character.transform.position.x;
}
if (character.transform.position.y > transform.position.y)
{
yDifference = character.transform.position.y - transform.position.y;
}
else
{
yDifference = transform.position.y - character.transform.position.y;
}
if (character.transform.position.z > transform.position.z)
{
zDifference = character.transform.position.z - transform.position.z;
}
else
{
zDifference = transform.position.z - character.transform.position.z;
}
if (xDifference >= movementThreshold || yDifference >= movementThreshold || zDifference >= movementThreshold)
{
moveTemp = character.transform.position;
moveTemp.y = 3;
transform.position = Vector3.MoveTowards(transform.position, moveTemp, speed * Time.deltaTime);
}
}
}
Your camera moves to height y = 3 whenever threshold is exceeded. Are your objects at that height?
Answer by Sethhalocat · Aug 16, 2015 at 08:23 AM
I don't think a follow script should be this complicated and/or be necessary. If you want your camera to all ways be looking at your character, all you have to do is bring the camera into the character section. If this isnt exactly what you are looking for you may have to settle and im sorry for that, or you can wait for another answer but I believe that this dosnt need to be as complicated
Please Like this comment and reply if it works/dosn't work as for it will help me a ton in the future. Thank you for taking time to read this and I hope it works for you.
-Seth Albertus
Answer by Jaws959 · Aug 16, 2015 at 09:16 AM
Try using this for your camera follow script
using UnityEngine; using System.Collections;
public class CameraFollow : MonoBehaviour { // Apply the Transform of the gameObject desired to view // Attach the transform from via the editor public Transform myTarget;
// Update is called once per frame
void Update ()
{
if (myTarget != null)
{
Vector3 targPos = myTarget.position;
targPos.z = transform.position.z;
transform.position = targPos;
}
}
}
Your answer
Follow this Question
Related Questions
Trouble with logic related to my camera smoothing 0 Answers
Rotate Camera to the object 0 Answers
Multitouch problem 0 Answers
Camera movement doesn't work in Unity (but does in builds) 0 Answers
How move camera without move selection? 0 Answers