[Solved] Follow Camera bug

Hello,

I’ve got some problems with my 2d camera follow.
I use this script to follow my player:

using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start () {
targetPos = transform.position;
}

// Update is called once per frame
void FixedUpdate () {
if (target)
{
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;

Vector3 targetDirection = (target.transform.position - posNoZ);

interpVelocity = targetDirection.magnitude * 5f;

targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);

transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

}
}
}

// Original post with image here > Unity 2d Camera Follow Script - Unity3diy - Posts about Unity Asset Store, Tuts, Solutions, Errors

As you can see I didn’t make it myself.


Some other settings:


The problem is that when I start the game, the camera moves to a specific spot automaticly instead of following the player, like this:

Thank you very much for helping,

  • Jip

You’re making the camera follow itself with an offset which is why its moving to the north-east.

You need to attach the Follow Camera script to the player game object. You should also rename it Follow Player.

Thankyou very much for the reply.
I placed the script at my player with the excact same settings, but now my player does this:

https://gyazo.com/06af37a5361044ce4973f62bcbdd1902

As you can see the player flies to the place where the camera also was flying to. I don’t know what to do with offset

I don’t know what to do. I am really new to unity and codes.
I do see the bug at the bottom, I don’t know if it’s something that made player fly.

Thank you

start with easy script, then you can make it more complicated :slight_smile:

public GameObject target;
void Update () {
transform.position = target.transform.position;
}

and i have tested your script, it works. Try to delete the “missed” script, maybe something is wrong with it. Or make new scene and test your camera there.

Thank you for the reply. Making a new scene or remove the missed script both didn’t work. I want to try out the code you sent, but I am really noob with this so first I need to know how to put that code in a script, than later I can do it myself.
So can you send me with every line the code you sent? (so just the whole script)
Because i don’t know where to put the lines you sent.

Thank you :smile:

Have you done the tutorials? Sounds like the script is on the wrong thing & you aren’t assigning stuff correctly in the inspector.

But I didnt make the robotguy myself it is a standard asset so I didn’t put anything in the inspector myself, only the camera.

The camera script goes on the target. It has a public game object that it follows so in the inspector you drag what that is into the slot.

I don’t really understand you, because I am not English. What you try to say is that as example the camera script needs to be placed on the the target (player) and you select the target and that is also the player? like this?
https://gyazo.com/06b06783d0c173e92e1323482f4af352

  1. i have placed your script on main camera, and then set player as target.

  2. you can make follow camera without script. Make it child of player (just drag&drop main camera on player in hierarchy). Dont forget to delete unused scripts from camera.

  3. add c# script with name FollowCam. Drop it on main camera.

using UnityEngine;
using System.Collections;

public class FollowCam : MonoBehaviour {
//and here you can insert easy script

//reference to gameobject, that should be followed
public GameObject target;

// every frame set camera position on target position
void Update () {
transform.position = target.transform.position;
}
}

if this script is working for you, then you can change it. Maybe with Vector3.Lerp

1 Like

Hi, thanks for the reply.
I tried to set the camera as child of player. That worked (finally SOMETHING works), but it doesn’t look really good.
Then I tried step 1 and 3 (I think that it is the same?), when i did that, this happend again:
https://gyazo.com/1a9f16c7cc50777918a66b1d0327b597

You can check if I did it right, I have no idea what this is.

Thnx

can you check this ?

using UnityEngine;
using System.Collections;

public class FollowCam : MonoBehaviour {
//and here you can insert easy script

//reference to gameobject, that should be followed
public GameObject target;

// every frame set camera position on target position
void Update () {
Vector3 pos = new Vector3 (target.transform.position.x, target.transform.position.y, -10);
transform.position = pos;
}
}
  1. i have tested your script, and the script is working. But i cannot understand what is wrong with your scene.

try to delete all camera scripts from your player. I think you have more then one script for camera moving in scene.

1 Like

Nope same problem. I only use standard assests, it isn’t th player because other targets and the problem is the same and I turned of everything in camera but also didn’t work.

It might be better if you put this aside for a while & do the tutorials. You can watch the videos & keep going back to see what they have done. Actually seeing it might be easier for you. Good luck

Thanks for your advice, but knowledge maybe doesn’t matter, because this might be a bug.

I doubt it’s a bug, vakabaka said above that they used the scripts supplied & had no issues

Can you please check if there is something wrong with my camera?

https://gyazo.com/e72169c27a79bd384d29a9ed4df43ad1

Because there is no problem with my player so i don’t understand what it is. Btw. its an android build.

Maybe this is something?

https://gyazo.com/9303dbc8bfbf5cd5c3407108f5998f97

I made a new project and a new player and the camera actually worked.

One question: How can I let the y as be always at the same spot like the z as. You wrote down -10 right there and I want the y as -0.8. When I wrote that down there like the z as I got errors.

The script is over riding what you are typing into the inspector. You need to fix the script.

Again, I STRONGLY suggest you stop & go do the tutorials. At some point people will stop doing the code for you & you will have to do it yourself so start easy with the tutorials so you can build up your knowledge base.

This is soooo wrong, knowledge does matter. As you found out, the script did work but you had something else wrong with your scene that you couldn’t resolve.