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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Player Movement issue - separating input and physics

Discussion in 'Scripting' started by jwalden, Apr 18, 2018.

  1. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    Hi guys,

    Is there a good way to split up player input and player physics?
    I'd like my input to be running through Update, but all my physics (based on that input) running through Fixed Update.

    Haven't found much info on this surprisingly (perhaps I've been looking in the wrong places) but any links or general advice/examples would be appreciated.

    Thanks.
     
    Meerah29 likes this.
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I don't think there is much to it other than not using local variables to collect input. You see a lot of coders doing things like:
    Code (csharp):
    1.  
    2. if(Input.GetAxis("Vertical"){
    3. //doSomething
    4. }
    5.  
    And then they check for it again a couple lines later. If you look in some the examples in scripting they do this:
    Code (csharp):
    1.  
    2. float vert;//usually this is local, but you would want it like this
    3. void Update(){
    4. vert = Input.GetAxis("Vertical");
    5. }
    6. void FixedUpdate(){
    7. if(vert){
    8. //do something;
    9. }
    10. }
    11.  
    It's probably only going to matter on a keyDown type of call, in which case you might want to only set it true in update and set it false in fixedUpdate.
     
    Last edited: Apr 18, 2018
    Meerah29 and _protagonist like this.
  3. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    Hi mate thanks for your response.

    With the if (vert) - I am getting an error. It seems to think this is a boolean.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I should have used:
    Code (csharp):
    1.  
    2. if(Input.GexAxis("Vertical")!=0){
    3. //doSomething
    4. }
    5.