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

[FIXED]Best way to move a RigidBody2D and a camera Script to follow him?

Discussion in 'Scripting' started by Urosq, Jan 16, 2016.

  1. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    Hi guys, i have to post a new thread cause i just cant fix this problem.

    So the thing is, i am bulding a 2D platformer game, like Geometry dash, where you can only control the Jump. So i have created a script for moving the player non stop on the X and adding a velocity to rigidbody2d on the Y when the player taps the screen. Its all working fine but here is the problem.

    When i use a camera that follows him just on the X axis, its fine, everything works as it should. Now when i use the same camera to follow him on the Y it works too, but it moves with him everytime i jump, and that is just weird, it makes you dizzy from looking at it.

    Now i have used a bunch of different scripts, one with Smoothing and one with deadzones and the last one is great, when you hit a certain height it justs moves the camera to follow you on Y and when you go down it reverts to normal, so the camera doesnt jump everytime you jump so to speak.

    Now the issue is when i use any camera that has Smoothing or Deadzones or any camera that doesnt follow the exact movement of the player, i get lags on my movement. Its like he is teleporting back and forth a little, it looks real bad, and i tried everything.

    Using FixedUpdate, LateUpdate, Smooth time for Delta time etc

    I even tried animating the camera movement, i even buyed the Camera Path hoping it would fix it, but no, its still the same. Any Damping or Smoothing or anything when the camera is not the same speed as the player he starts lagging.

    Well thats the best explanation i can give you without a video.

    So can it be that my movement script is wrong? This is the part of the script for moving the player on the X.

    Code (csharp):
    1.  
    2.             Vector3 temp = transform.position;
    3.             temp.x += forwardSpeed * Time.smoothDeltaTime;
    4.             transform.position = temp;
    I just cant find a fix for this lagging. So can you guys tell me, if there is a better way to move the player, and a way to stop the lagging.

    Keep in mind that i am new to programming and Unity. Just over a month and a half has passed since i started and this is a real problem and a first one i can not fix.

    Thanks guys in advance! :)

    EDIT: Fixed it. Using AddForce to move player, calling it in FixedUpdate, turning on Interpolate and using a standard assets camera did the trick. Jittering is not the issue anymore, everything is working as it should! :)
     
    Last edited: Jan 16, 2016
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Urosq likes this.
  3. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    Hmmm, never thought of that, to move everything and keep the player in the same spot. Interesting. Any way you can quickly explain how that would work, and its pros and cons?

    Thanks for the link, but i managed to half fix it. Setting the RigidBody of my Player to Interpolate almost did the trick. now there is no more jittering except a little one that occurs every couple of seconds, but that one is barely noticable so its ok.

    Now i am going to re learn everything about the camera and create a new script to work for it. Any links on that, maybe you will share something i did not find and read?

    Just the issue now is that my player moves back and forward, like he slows down a bit and then accelerates but never out of bounds of the camera, movement is really minor but noticable so i think that issue is related to the camera it self hence the relearning and rewriting
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    what's the script for your camera?
     
  5. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    This is the script. I found it on the internet, pieced a couple of stuff together and this is what i got. Now i think its not working as it should, because it wont follow my player even though tags are set correctly. Anyway i will redo everything, start the whole project again. Refresh is what i need, too much clutter has accumulated.

    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4.  
    5. namespace UnityStandardAssets._2D
    6. {
    7.     public class CameraFollow : MonoBehaviour
    8.     {
    9.         public float xMargin = 1f; // Distance in the x axis the player can move before the camera follows.
    10.         public float yMargin = 1f; // Distance in the y axis the player can move before the camera follows.
    11.         public float xSmooth = 8f; // How smoothly the camera catches up with it's target movement in the x axis.
    12.         public float ySmooth = 8f; // How smoothly the camera catches up with it's target movement in the y axis.
    13.         public Vector2 maxXAndY; // The maximum x and y coordinates the camera can have.
    14.         public Vector2 minXAndY; // The minimum x and y coordinates the camera can have.
    15.  
    16.         private Transform m_Player; // Reference to the player's transform.
    17.  
    18.  
    19.         private void Awake()
    20.         {
    21.             // Setting up the reference.
    22.             m_Player = GameObject.FindGameObjectWithTag("Player").transform;
    23.         }
    24.  
    25.  
    26.         private bool CheckXMargin()
    27.         {
    28.             // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
    29.             return Mathf.Abs(transform.position.x - m_Player.position.x) > xMargin;
    30.         }
    31.  
    32.  
    33.         private bool CheckYMargin()
    34.         {
    35.             // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
    36.             return Mathf.Abs(transform.position.y - m_Player.position.y) > yMargin;
    37.         }
    38.  
    39.  
    40.         private void Update () {
    41.  
    42.         TrackPlayer();
    43.         }
    44.  
    45.     private void TrackPlayer()
    46.         {
    47.             // By default the target x and y coordinates of the camera are it's current x and y coordinates.
    48.             float targetX = transform.position.x;
    49.             float targetY = transform.position.y;
    50.  
    51.             // If the player has moved beyond the x margin...
    52.             if (CheckXMargin())
    53.             {
    54.                 // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
    55.                 targetX = Mathf.Lerp(transform.position.x, m_Player.position.x, xSmooth*Time.deltaTime);
    56.             }
    57.  
    58.             // If the player has moved beyond the y margin...
    59.             if (CheckYMargin())
    60.             {
    61.                 // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
    62.                 targetY = Mathf.Lerp(transform.position.y, m_Player.position.y, ySmooth*Time.deltaTime);
    63.             }
    64.             // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
    65.             targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
    66.             targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
    67.  
    68.             // Set the camera's position to the target position with the same z component.
    69.             transform.position = new Vector3(targetX, targetY, transform.position.z);
    70.         }
    71.     }
    72. }
     
    Banditmayonnaise likes this.
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Urosq likes this.
  7. Urosq

    Urosq

    Joined:
    Dec 13, 2015
    Posts:
    15
    I fixed it! Using AddForce to move my player, calling it in FixedUpdate and turning Interpolate did the trick! Camera i was using is from standard assets and it works like a charm. Thank you all for your inputs, the links were really usefull! :)
     
  8. adhamBer

    adhamBer

    Joined:
    Feb 14, 2015
    Posts:
    3
    Hi,hey man can you please help me out here. I am stuck on the same problem you were. Can you please explain what you did? and what camera follow script you are using? did you use the standard one in unity? smooth camera follow 2d? I've been stuck on this forever ... more than 5 months... please help me out! thanks in advance!