Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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.