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;
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.
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
i have placed your script on main camera, and then set player as target.
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.
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
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.
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;
}
}
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.
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
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.