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

Gravity not working because of moventscript

Discussion in 'Scripting' started by xxfelixlangerxx, Apr 5, 2019.

  1. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    Hey
    there is a problem with my game. Its a unity physics / scripting problem. My player falls really slow. Its because of the script but how can i move my player then?


    Code (CSharp):
    1. {
    2.     public GameObject playerlight;
    3.     public GameObject playerdark;
    4.     public Joystick PlayerMovementJoystick;
    5.     public Rigidbody2D playerDarkRigidbody;
    6.     public Rigidbody2D playerLightRigidbody;
    7.     public float movespeed;
    8.     public Transform playerLightpos;
    9.     public Transform playerDarkpos;
    10.     public float jumpforce;
    11.  
    12.     private void Start()
    13.     {
    14.         playerlight = GameObject.Find("PlayerLight");
    15.         playerdark = GameObject.Find("PlayerDark");
    16.     }
    17.  
    18.     public void SwitchPlayerS()
    19.     {
    20.     }
    21.  
    22.     private void Update()
    23.     {   if(GamaDataManagerScript.LightOn == true)
    24.         {
    25.             Vector3 playermovementvec = new Vector3(PlayerMovementJoystick.Horizontal * Time.deltaTime * movespeed, 0, 0);
    26.             playerLightRigidbody.MovePosition(playermovementvec + playerLightpos.position);
    27.         }
    28.         else
    29.         {
    30.             Vector3 playermovementvecdark = new Vector3(PlayerMovementJoystick.Horizontal * Time.deltaTime * movespeed, 0, 0);
    31.             playerDarkRigidbody.MovePosition(playermovementvecdark + playerDarkpos.position);
    32.         }
    33.     }
    34.  
    35.     public void Jump()
    36.     {
    37.         if(GamaDataManagerScript.LightOn == true)
    38.         {
    39.             playerLightRigidbody.velocity.Set(playerLightRigidbody.velocity.x, playerLightRigidbody.velocity.y + jumpforce);
    40.         }
    41.     }
    42. }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    Add a gravity (+ gravity * Time.deltaTime) to your playermovement vector.

    Note that its better to move rigidbodies in the FixedUpdate to avoid potential issues.
     
  3. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    Isn´t time.deltaTime the same?
    Thanks
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Rigidbodies are moved using the physics system. The physics update always takes place immediately following FixedUpdate.

    Update on the other hand is bound to your graphics frame rate, which can be called 0 times or many times between physics updates.

    So FixedUpdate exists so you have a place to put any code which uses physics, and ensure it is called exactly 1 time and immediately before the physics update. All the physics related movement stuff is designed with this in mind, so you shouldn't be surprised if you get some unexpected results calling it elsewhere. I doubt Unity QA even bothers regression testing the physics related stuff in Update prior to release.