Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D camera not following player correctly help pls?

Discussion in 'Getting Started' started by KaiDevX, Feb 28, 2023.

  1. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    so i made(copied) a camera follow script off of YouTube but the camera didn't show the show anything, i checked that the camera was -10 in z axis and it was its still didn't help, but then I deleted the old one and copied a new script but the same problem occurred, so I tested but this time I looked in the scene view and moved the player ,manually to find out the camera moves in the opposite direction the player is moving

    when i move right the camera goes up
    when i move left the camera goes down when i jump upward the camera goes to the right and when i fall it moves back to the left

    it also doesnt display my tiles from my tile map and only the player any suggestions?

    my script:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraController : MonoBehaviour
    { // variable to link camera and player
    public GameObject player;




    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {// update the camera position to the player
    transform.position = new Vector3(player.transform.position.y, player.transform.position.x, player.transform.position.z);
    }
    }

    summary: 2 issues:
    cant see tiles on screen
    camera moves in wierd direction opposite of the player


    1st = scene view
    2nd = game view
    2d platformer scene view.png 2d platformer game view.png 2d platformer scene view.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    Otherwise, here is how to debug your code:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. patrickjarnfelt

    patrickjarnfelt

    Joined:
    Jun 24, 2013
    Posts:
    28
    @KaiDevX You have swapped the y, and x in the camera follow script. Maybe that would fix everything.
     
  4. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    ok ill try that ty!
     
  5. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    i still have the problem where i cant see anything on my screen so i deleted the vector 3 and replaced it with vector 2 and delected the player.position.z cause it thought the camera was snapping to the player and it was causing the game scene to not see anything but it still didnt help
     
  6. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I'm not 100% sure what's happening just by looking at your screenshots. Could be the tilemap is at the wrong z position, could be something else. Your scene is very simple, and Unity tends to work well out of the box, so something has to be messed up somewhere. We could keep asking for information for another few days, but the fastest thing would be to zip your project and link it here. I don't mind downloading it and having a look.

    Google drive is the easiest hosting site to use in my opinion.

    Also, in the future, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    It wasn't the best response here; KaiDevX is obviously very new and likely won't absorb all that information, but saying he's spamming and gatekeeping is a bit overkill. His post contains a ton of useful information on how to debug, which is exactly what KaiDevX needs to do here. Kurt-Dekker is one the most active users on the forums, he's helped many, many people, and has provided a ton of useful information. I often link to his posts when I see people with the same basic questions (how to fix NullReferenceException for example).
     
  7. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    it says its too large when trying to upload the project
     
  8. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Windows:
    Right click the project folder -> Send to -> Compressed (zipped) folder

    This should cut the size down to about half. Google drive has an upload limit of 750 GB's per day, and the file should be less than 1 GB. I'm talking about google drive, not gmail. You can only store 15 GB of data at a time on your account, so make sure it isn't full.

    I recommended google drive, but you can use whatever you like.
     
  9. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    so how do i give it to you through google drive?

    i've never used google drive tbh
     
    Last edited: Mar 2, 2023
  10. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I do not mean to be rude here, but you have to put in at least some amount of effort when asking for help. If you'd rather not give your project, you can post more details about your issue (images, videos, etc...).

    I'm not going to write a guide on how to use google drive; this is incredibly basic information that anyone can find with a quick google search.
     
  11. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    mb
     
  12. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    how will this help if you cant access my google drives tho? i did the research on google but this isn't clicking in my head
    on how this will help
    nothing said anything about SHARING files
     
  13. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Right click on the file you want to share -> Click "Get link" -> Click on "Restricted" and change it to "Anyone with the link" -> Click "Copy link" -> Paste the link here.
     
  14. KaiDevX

    KaiDevX

    Joined:
    Jan 18, 2023
    Posts:
    8
    alr well... sry to waste your time but i figured it out so you guys are free to go! lol ( i technically saved u time bc you dont have to go through my project ig) sry tho bc im so goofy and 1 IQ