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. Dismiss Notice

Float variable problem

Discussion in 'Scripting' started by pokefan199, Feb 23, 2016.

  1. pokefan199

    pokefan199

    Joined:
    May 14, 2014
    Posts:
    4
    Hey I'm having trouble with variables... I'm very new to coding but I'm trying to make a basic side-on 2d movement script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     void FixedUpdate()
    7.     {
    8.         //Variables
    9.         float MoveHorizontal = Input.GetAxis("Horizontal");
    10.  
    11.         if (Input.GetKeyDown("space"))
    12.         {
    13.             float MoveVertical = 1;
    14.         }
    15.  
    16.         Vector2 Movement = new Vector2(MoveHorizontal, MoveVertical);
    17.  
    18.         //Actions
    19.         this.GetComponent<Rigidbody2D>().AddForce(Movement);
    20.     }
    21.  
    22. }
    23.  
    But the variable MoveVertical only exists within the if loop and cant be used in the rest of the code. Help?
     
  2. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    You don't need to set the value to MoveVertical every frame you press space + no one can access it outside of code block, declare it and set the value under your class name. The whole class strucute is katastrophally (don't worry I wasn't better when I remember back at my beginning). However you should learn C# basics and Unitys API. To your movement "problem" it's better to try to slove basic problems by your self I would suggest you to check out the unitys free 2D platformer and look into their scripts how all works :)
     
  3. pokefan199

    pokefan199

    Joined:
    May 14, 2014
    Posts:
    4
    Thanks! But these things arent called each frame, they are called 50 times a second, I used fixedupdate. Also, I'd love to hear your tips on cleaning up the script. This is as tidy as it gets from me.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Code (csharp):
    1. float moveVertical = 0f;
    2. if(Input.GetKeyDown("space"))
    3. {
    4.     moveVertical = 1f;
    5. }
    6.  
    7. ...
    A pair of brackets (these ones: {} ) create what's called a scope. Things declared within the scope can see things outside of it, but things outside the scope can't see what's inside of it.

    So, if you declare the moveVertical outside of the if-statement's scope, you can use it both inside and outside of that scope.

    The code results in you saying "If the space is down, add force 1 upwards. Otherwise, add force 0 upwards".


    Note that it's not recommended to read Input in FixedUpdate. The Input is update every Update, and you might get several Update calls per FixedUpdate (or several FixedUpdate calls per Update, if your framerate is lower than the fixed timestep). GetKeyDown is only true on exactly the Update where the space is pressed, so there's a good chance that GetKeyDown will have been set to false again when FixedUpdate comes around:

    Update
    Update
    FixedUpdate
    Update -> space is pressed, GetKeyDown("space") becomes true
    Update -> GetKeyDown becomes false one frame later
    FixedUpdate -> GetKeyDown("space") is checked, it's false. Character does not jump. Everyone is sad.

    An easy fix is to read the key in Update, and set a bool value to indicate if the character should jump on the next FixedUpdate, and then set that bool false again when it's used:

    Code (csharp):
    1. private bool jump;
    2.  
    3. void Update() {
    4.     if(Input.GetKeyDown("space")) {
    5.         jump = true;
    6.     }
    7. }
    8.  
    9. void FixedUpdate() {
    10.     float horizontalMovment = 0f;
    11.     if(jump) {
    12.         horizontalMovement = 1f;
    13.         jump = false;
    14.     }
    15.     ... // as before
    16. }
    Note that the jump field is declared outside the scope of both the Update and the FixedUpdate function, so it can be used in both of them. In that way, the brackets of a method and the brackets of an if-statement functions in the same way.

    Hope that helps!
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    If you want to use your variable outside the if block (btw: it's called a 'block' not a 'loop'), just declared it outside the block.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerMovement : MonoBehaviour {
    4.     void FixedUpdate()
    5.     {
    6.         //Variables
    7.         float MoveHorizontal = Input.GetAxis("Horizontal");
    8.         float MoveVertical = 0.0f;
    9.         if (Input.GetKeyDown("space"))
    10.         {
    11.             MoveVertical = 1.0f;
    12.         }
    13.         Vector2 Movement = new Vector2(MoveHorizontal, MoveVertical);
    14.         //Actions
    15.         this.GetComponent<Rigidbody2D>().AddForce(Movement);
    16.     }
    17. }