Search Unity

Camera movement issues

Discussion in 'Scripting' started by mate_veres, Oct 22, 2019.

  1. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    So I am trying to make a "crossy-road " style game. But I don't know how to handle smoothly when the player is moving to fast and trying to get out of the screen. Any idea?

    This is my solution for now.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class cameramovement : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public float speednormal;
    9.     public float speedrush;
    10.     private float subs;
    11.  
    12.     private void Start()
    13.     {
    14.         subs =Mathf.Abs(player.transform.position.x - transform.position.x);
    15.        
    16.     }
    17.  
    18.  
    19.  
    20.  
    21.     void Update()
    22.    
    23.     {
    24.        
    25.  
    26.  
    27.         if (Mathf.Abs(player.transform.position.x - transform.position.x) < subs+4)
    28.         {
    29.             transform.position += new Vector3(speednormal, 0, 0);
    30.            
    31.         }
    32.  
    33.         else
    34.         {
    35.            
    36.             transform.position += new Vector3(speedrush, 0, 0);
    37.            
    38.            
    39.         }
    40.  
    41.  
    42.  
    43.     }  
    44. }
    45.  
     
  2. cmyd

    cmyd

    Joined:
    Oct 29, 2017
    Posts:
    98
    First switch you move code from Update() to LateUpdate(), LateUpdate is called after the update.
    Second FPS can also cause these delays, Use Time.deltaTime Like this
    Code (CSharp):
    1. if (Mathf.Abs(player.transform.position.x - transform.position.x) < subs+4)
    2.         {
    3.             transform.position += new Vector3(speednormal * Time.deltaTime, 0, 0);
    4.          
    5.         }
    6.         else
    7.         {
    8.          
    9.             transform.position += new Vector3(speedrush * Time.deltaTime, 0, 0);
    10.          
    11.          
    12.         }
    Time.deltaTime is the time took from switching one frame to another.
     
  3. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Thank you!