Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Follow Camera bug

Discussion in '2D' started by Jip1912, Aug 22, 2015.

  1. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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 > http://unity3diy.blogspot.com/2015/02/unity-2d-camera-follow-script.html


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

    ---------------------------------------------------------------------------------------------------------------------------------------------
    Some other settings:

    https://gyazo.com/3aebb1348b47f2da26f246a065158671

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

    https://gyazo.com/0a4b038a8d7189f61b42e87d32de8971


    Thank you very much for helping,

    - Jip
     
  2. PatrickDuncan

    PatrickDuncan

    Joined:
    Jul 9, 2015
    Posts:
    11
    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.
     
  3. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    start with easy script, then you can make it more complicated :)

    Code (CSharp):
    1. public GameObject target;
    2. void Update () {
    3. transform.position = target.transform.position;
    4. }
    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.
     
    Last edited: Aug 23, 2015
  5. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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 :D
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you done the tutorials? Sounds like the script is on the wrong thing & you aren't assigning stuff correctly in the inspector.
     
  7. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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.
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
  9. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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
     
  10. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FollowCam : MonoBehaviour {
    5. //and here you can insert easy script
    6.  
    7. //reference to gameobject, that should be followed
    8. public GameObject target;
    9.  
    10. // every frame set camera position on target position
    11. void Update () {
    12. transform.position = target.transform.position;
    13. }
    14. }
    if this script is working for you, then you can change it. Maybe with Vector3.Lerp
     
    Last edited: Aug 24, 2015
    Jip1912 likes this.
  11. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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
     
  12. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    can you check this ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FollowCam : MonoBehaviour {
    5. //and here you can insert easy script
    6.  
    7. //reference to gameobject, that should be followed
    8. public GameObject target;
    9.  
    10. // every frame set camera position on target position
    11. void Update () {
    12. Vector3 pos = new Vector3 (target.transform.position.x, target.transform.position.y, -10);
    13. transform.position = pos;
    14. }
    15. }
    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.
     
    Last edited: Aug 24, 2015
    Jip1912 likes this.
  13. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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.
     
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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
     
  15. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Thanks for your advice, but knowledge maybe doesn't matter, because this might be a bug.
     
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I doubt it's a bug, vakabaka said above that they used the scripts supplied & had no issues
     
  17. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314


    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
     
    Last edited: Aug 25, 2015
  18. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314

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

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    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.
     
  20. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
  21. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    do you mean y- 0.8 ? c# can not count directly in new vector.

    so make something like:
    float yPos = target.transform.position.y - 0.8;
    Vector3 pos = new Vector3 (target.transform.position.x, yPos, -10);

    i think there is better way for that: just make new empty gameobject. Make it child of your player. And set it in editor, where your camera should be. Then target your camera on this gameobject. ANd check the z position of new object :D
     
  22. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I know ofcourse what you mean. But I am only 14 years old and I don't do this (making unity games) more than 1 hour a day. Most of the time I go make something in unity for half an hour. The tutorials take a lot of time and a lot of times they teach something I don't want to know. I am just testing out some things in unity. Maybe I can do the tutorials later.
     
  23. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Thanks a lot and thankyou for all other help!!! :D If you want, you can also help me with this (small) question
     
  24. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Honestly, that's a cop out excuse. If you are serious about making your own games then. Learn, otherwise if you want people to fix your scripts &/or write them for you post in the collaboration thread. If you spend the time now to learn how to code you will make games quicker later on, otherwise you will end up spending all your time begging people in this forum to do your work for you than it would've taken you to write it yourself.
     
    Jip1912 likes this.
  25. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Thankyou. I see that you just want to help me and that you are a wise man ^^ I will give it a try. Btw, how do I mark a comment on this thread as solved (best reaction)?
     
  26. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Yes, sorry if that came off as rude (i was being sworn at in another thread at the time for not doing someone's code for them). I just meant to say that it's more important to go slow than to try to make a game straight away because your first game will never be as good as it was when you imagined it so save the idea until you can do it justice.

    Good luck & have fun
    (Sorry again)
     
  27. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    No problem :D It's just good to say it like that. Btw. do you mean the project tutorials or the topic tutorials (all of them?)