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

Can not get any smooth player/camera movement

Discussion in 'Editor & General Support' started by JackMini36, Feb 16, 2014.

  1. JackMini36

    JackMini36

    Joined:
    Feb 13, 2014
    Posts:
    21
    Hello,

    I have been struggling with this for 2 days now with no solution. I am a total begginer in Unity and I am trying to make the simpliest 2D game. two platforms and one sphere moving and jumping on them. No textures, no animations, nothing..

    The problem is that when the player(sphere) moves there is a lot of flickering. I tried removing the camera movement but there is still some flickering. I tried a lot of things, read a lot of forums, but can not seem to even make it better.
    I used boxcollider2d for the platforms, rigidbody2d and circlecollider2d for the sphere.
    here are my scripts:

    PlayerController.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     [HideInInspector]
    8.     public bool facingRight = true;         // For determining which way the player is currently facing.
    9.     [HideInInspector]
    10.     public bool jump = false;               // Condition for whether the player should jump.
    11.    
    12.     public float moveForce = 365f;          // Amount of force added to move the player left and right.
    13.     public float maxSpeed = 5f;             // The fastest the player can travel in the x axis.
    14.     public float jumpForce = 500f;          // Amount of force added when the player jumps.
    15.  
    16.     private bool onGround = false;          // Whether or not the player is grounded.
    17.  
    18.     void Update()
    19.     {
    20.         // If the jump button is pressed and the player is grounded then the player should jump.
    21.         if(Input.GetKeyDown(KeyCode.W)  onGround)
    22.             jump = true;
    23.     }
    24.    
    25.    
    26.     void FixedUpdate ()
    27.     {
    28.         float h = Input.GetAxisRaw("Horizontal"); //get horizontal input
    29.  
    30.         // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
    31.         if(h * rigidbody2D.velocity.x < maxSpeed)
    32.         {
    33.             rigidbody2D.AddForce(Vector2.right * h * moveForce);
    34.         }
    35.        
    36.         // If the player's horizontal velocity is greater than the maxSpeed...
    37.         if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
    38.         {
    39.             // ... set the player's velocity to the maxSpeed in the x axis.
    40.             rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
    41.         }
    42.  
    43.         // If the input is moving the player right and the player is facing left...
    44.         if(h > 0  !facingRight)
    45.         {
    46.             Flip();
    47.         }
    48.         // If the input is moving the player left and the player is facing right...
    49.         else if(h < 0  facingRight)
    50.         {
    51.             Flip();
    52.         }
    53.         // Otherwise if no horizontal input is given, stop moving.
    54.         else if(h==0)
    55.         {
    56.             rigidbody2D.velocity =  new Vector2(0, rigidbody2D.velocity.y );
    57.         }
    58.        
    59.         // If the player should jump...
    60.         if(jump)
    61.         {
    62.             // Add a vertical force to the player.
    63.             rigidbody2D.AddForce(new Vector2(0f, jumpForce));
    64.            
    65.             // Make sure the player can't jump again until the jump conditions from Update are satisfied.
    66.             jump = false;
    67.             onGround = false;
    68.         }
    69.     }
    70.  
    71.     void Flip ()
    72.     {
    73.         // Switch the way the player is labelled as facing.
    74.         facingRight = !facingRight;
    75.        
    76.         // Multiply the player's x local scale by -1.
    77.         Vector3 theScale = transform.localScale;
    78.         theScale.x *= -1;
    79.         transform.localScale = theScale;
    80.     }
    81.  
    82.     void OnCollisionEnter2D(Collision2D colInfo)
    83.     {
    84.         if(colInfo.collider.tag == "Ground")
    85.         {
    86.             onGround = true;
    87.         }
    88.  
    89.     }
    90. }
    91.  
    92.  
    CameraFollow.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public float xMargin = 1f;      // Distance in the x axis the player can move before the camera follows.
    8.     public float yMargin = 1f;      // Distance in the y axis the player can move before the camera follows.
    9.     public float xSmooth = 8f;      // How smoothly the camera catches up with it's target movement in the x axis.
    10.     public float ySmooth = 8f;      // How smoothly the camera catches up with it's target movement in the y axis.
    11.     public Vector2 maxXAndY;        // The maximum x and y coordinates the camera can have.
    12.     public Vector2 minXAndY;        // The minimum x and y coordinates the camera can have.
    13.  
    14.  
    15.     private Transform player;       // Reference to the player's transform.
    16.     //private GameObject gameManager;
    17.  
    18.  
    19.     void Awake ()
    20.     {
    21.         // Setting up the reference.
    22.         //gameManager = GetComponent<GameManager>();
    23.         player = GameObject.FindGameObjectWithTag("Player").transform;
    24.     }
    25.  
    26.  
    27.     bool CheckXMargin()
    28.     {
    29.         // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
    30.         return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    31.     }
    32.  
    33.  
    34.     bool CheckYMargin()
    35.     {
    36.         // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
    37.         return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    38.     }
    39.  
    40.  
    41.     void FixedUpdate ()
    42.     {
    43.         TrackPlayer();
    44.     }
    45.    
    46.    
    47.     void TrackPlayer ()
    48.     {
    49.  
    50.         // By default the target x and y coordinates of the camera are it's current x and y coordinates.
    51.         float targetX = transform.position.x;
    52.         float targetY = transform.position.y;
    53.  
    54.         // If the player has moved beyond the x margin...
    55.         if(CheckXMargin())
    56.             // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
    57.             targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);
    58.  
    59.         // If the player has moved beyond the y margin...
    60.         if(CheckYMargin())
    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, 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.  
    Any ideas what should I do?