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

RigidBody Ignores If Statement

Discussion in 'Physics' started by lpflicke, Oct 28, 2015.

  1. lpflicke

    lpflicke

    Joined:
    Oct 28, 2015
    Posts:
    1
    Hi, I'm very new to unity and started programming in javascript.
    This is probably a very dumb mistake but what I'm trying to do is to make the rigidbody attached to the cube move along the y axis when I click "W".Instead when I press Start, It goes up straight away without me clicking "W"
    Here is my code:

    #pragma strict

    var rb: Rigidbody;

    function Start () {
    rb = GetComponent.<Rigidbody>();
    }

    function FixedUpdate () {

    if (Input.GetKeyDown(KeyCode.W));{
    rb.velocity = Vector3(0,10,0);
    }

    }
    Thank you in advance.
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    You have an extra semicolon in your if statement:

    if (Input.GetKeyDown(KeyCode.W));{

    It's kind of an esoteric issue, but that's an "empty statement", which becomes the thing the if statement is actually running or not running. As a result, it means the following code (the velocity change) is essentially on its own line and always being run.