-1

I am a total beginner at Unity3d. I have some background in android programming, but no C# experience whatsoever. The first thing I am trying to do is to create a clone of flappy bird game, called flappy plane, according to this tutorial http://anwell.me/articles/unity3d-flappy-bird/

The problem is, when I tried to write a script that allows player to move (player.cs) with the code

using UnityEngine;

using System.Collections;


public class player: MonoBehaviour {



public Vector2 jumpForce = new Vector2(0,300);

public Vector2 jumpForce2 = new Vector2(0,-300);
// Use this for initialization


// Update is called once per frame

void Update () {


if (Input.GetKeyUp("space")){

    Rigidbody2D.velocity = Vector2.zero;

    Rigidbody2D.AddForce(jumpForce);


 }

}
}

I get an error "An Object reference is required to access non-static member 'UnityEngine.Rigidbody2D.velocity'". I have googled that and it is suggested to access Rigidbody2d with GetComponent().velocity,

so I changed

    Rigidbody2D.velocity = Vector2.zero;

    Rigidbody2D.AddForce(jumpForce);

with

   GetComponent<Rigidbody2D>().velocity = Vector2.zero;

   GetComponent<Rigidbody2D>().AddForce(jumpForce);

The error is gone and I am able to add the script to the object, still I don`t get the desired action - after I hit play the object turns invisible and just falls down, does not react to spacebar button. What am I doing wrong? Thanks for the answer.

Gregory
  • 31
  • 6

2 Answers2

1

It's possible that you're not adding enough force to have the object move upwards.

There's technically nothing wrong with your code. (Although you do have somethings mixed up in your question). The problem is in the fact that you're not adding ANY force upwards every single frame.

Essentially, at the moment, your player object is in free-fall the instant you hit the play button, and you're adding a minuscule force to the player only on the frames that the space bar is pressed.

To solve this, here's what you should be doing

  1. Add an upward force to counter-act the force of gravity every frame. You can do this in two ways. a. Set the rigidbody's velocity.y to 0 BEFORE detecting the space bar (this is really a hacky way, but it'll suffice and doesn't need any more code)

    b. Add an upward force to the player which will nullify the effect of gravity. Just use F = mg to get the value of force you'd need to add.

  2. You could, alternatively set the isKinematic property to true by default on the Player's rigidbody, set it to false on pressing the space bar, and back to true after a few frames (5 - 6 frames)

Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25
0

make sure your player object and the ground both have BoxCollider2D colliders to keep above ground.

you could keep a reference stored for the rigidBody like Rigidbody2D myRigidbody;

then in start put myRigidbody = GetComponent<Rigidbody2D>(); then you would use like myRigidbody.AddForce(jumpForce); your jumpForce2 though is shooting your player downward you should not need it in a jump as the physics and gravity will apply with the rigidbody.

incase your input is not set up in the project settings try to fire the jump with

Input.GetKeyDown(KeyCode.Space);
Justin Markwell
  • 341
  • 6
  • 13