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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Tutorial roll-a-ball help

Discussion in 'Scripting' started by Ermergerdwesleh, Oct 22, 2015.

  1. Ermergerdwesleh

    Ermergerdwesleh

    Joined:
    Oct 22, 2015
    Posts:
    2
    Hello I have just started using unity and was trying to complete the tutorial to make a game where the player moves a ball. I ran into a problem while writing the script and can't get the ball to move when I press play.
    here is the script

    public class PlayerController : MonoBehaviour {
    public float speed;
    private Rigidbody rb;
    void start()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {

    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.AddForce(movement * speed);
    }
    }
    I would appreciate the help, thanks!
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    You should make the two floats properties, and move Input.GetAxis() into Update().

    FixedUpdate() should only be used for physics, and nothing else. Use Update() and LateUpdate() for testing things such as input.

    Also Start() should be uppercase.

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.     public float speed;
    3.     private Rigidbody rb;
    4.  
    5.     float moveHorizontal, moveVertical;
    6.     Vector3 movement;
    7.  
    8.     void Start()
    9.     {
    10.         rb = GetComponent<Rigidbody>();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         moveHorizontal = Input.GetAxis("Horizontal");
    16.         moveVertical = Input.GetAxis("Vertical");
    17.      
    18.         movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.         rb.AddForce(movement * speed);
    24.     }
    25. }
     
    Last edited: Oct 22, 2015
  3. Ermergerdwesleh

    Ermergerdwesleh

    Joined:
    Oct 22, 2015
    Posts:
    2
    Thanks you for your help! has the way unity uses scripts changed? because in the video it had no trouble reading the script, also I'm very new to programming in general and was wondering if there were any up to date tutorials that help teach unity/c#? Thanks a bunch!
     
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    As you get further into unity, you will see that it is best to put it into the Update instead of the FixedUpdate. But what the main issue is, is that you have a lowercase start(), when it should be an uppercased Start(). So you probably have a null reference to the Rigidbody.
     
    GoofBall101 and Ermergerdwesleh like this.
  5. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    Heres a link for programming in C#. Its the whole language.



    but look on the Unity go to Tutorials - Scripting - And watch most. The most valuable ones in programming are ,

    if else statements

    voids , Voids make different (Functions) So say I tell this void number "A" it moves a ball 1 every second. I make another void that dose something but needs the ball to move. You call the void and it reads void "A" then goes on with the code.

    var, floats, bools, ints, strings

    void Update, Start, and the other ones Unity has

    These are what I use most of the time when programming! GL