Help 2D movement up

Dear,

I am making a flappy bird clone in unity.
I wan’t to make mu sprite going up smoothly when i click i use this code:

if (alive == 1){
			rigidbody.AddForce(Vector2.right * speed * 3);
			rigidbody.AddForce(Vector3.down * fallspeed);
			if(Input.GetMouseButtonDown(0)){
				rigidbody.AddForce(Vector3.up * upspeed);
			}
		}

This works :smile:
But when i click my sprite is going up but not smooth he jumps up how to het it smooth?

Perhaps it is because each frame when mouse button is down, you are applying a force down and up. I would pace your if() above the stuff not in if and add in a return.

To move smoothly multiply your move speed in the method by time.deltaTime()

Tanks for the help but it still doesn’t work my new code:

if (alive == 1){
			rigidbody.AddForce(Vector2.right * speed * 3);
			if(Input.GetMouseButtonDown(0)){
				rigidbody.AddForce(Vector2.up * upspeed * Time.deltaTime);
			} else {
				//rigidbody.AddForce(Vector3.down * fallspeed);
			}
		}

Try to multiply by Time.DeltaTime in every AddForce you have! Not only when the button is pressed.

Now my plane doesn’t move at all new code:

if (alive == 1){
			rigidbody.AddForce(Vector2.right * speed * Time.deltaTime);
			if(Input.GetMouseButtonDown(0)){
				rigidbody.AddForce(Vector2.up * upspeed * Time.deltaTime);
			} else {
				//rigidbody.AddForce(Vector3.down * fallspeed * Time.deltaTime);
			}
		}

ok avoid what i said before.
You mean that goes up like flickering movement!? is there gravity ?

I made a Test here!
I created a new project and just apply this script (Below) to an brand new cube. It jump so smotth.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour {
  public float _Jump = 1000f;
  void Update () {
    if (Input.GetMouseButtonDown(0)) {
      Debug.Log("Jumping...");
      rigidbody.AddForce(Vector3.up * _Jump);
    }
  }
}

Maybe something else on your code or another script is messing with your object.
Maybe the problem is that you using Vector2 and not Vector3, since its Rigidbody and not Rigidbody2D. I don’t know!

I made a quick flappy bird clone last night, here is what I did:

public float rotationSpeed;
void Update () {
float step = rotationSpeed * Time.deltaTime;
if(MovingUp == false) {
	rigidbody2D.velocity = new Vector2(0, -1);
	transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, 270), step);
}

if(Input.GetButtonDown("Fire1") ) {
	rigidbody2D.velocity = new Vector2(0, 2);
	transform.rotation = Quaternion.Euler(0, 0, 25);
	MovingUp = true;
	StartCoroutine(delayDown());
}
}

IEnumerator delayDown () {
	yield return new WaitForSeconds(0.1f);
	MovingUp = false;
}

This is my full code:

using UnityEngine;
using System.Collections;

public class game : MonoBehaviour {

	public float speed = 15.0F;
	public int start = 0;
	public int alive = 0;
	public float fallspeed = 14.0F;
	public float upspeed = 14.0F;

	// Use this for initialization
	void Start () {
		GameStart();
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0)){
			if(start == 1){
				Destroy(GameObject.Find("tapLeft"));
				Destroy(GameObject.Find("tapRight"));
				Destroy(GameObject.Find("tapTick"));
				start = 0;
				alive = 1;
			}
		}
		if (alive == 1){
			rigidbody.AddForce(Vector2.right * speed);
			if(Input.GetMouseButtonDown(0)){
				rigidbody.AddForce(Vector2.up * upspeed);
			} else {
				rigidbody.AddForce(Vector3.down * fallspeed);
			}
		}
	}

	public void GameStart(){
		alive = 0;
		start = 1;
	}
}

And this is my bird:

So it is a Sprite.
Try to change this:

rigidbody

to this:

rigidbody2D

And my advice:

Let’s see if this work.

Still having this problem

I see… The main problem is that addForce(Vector3.Down); remove it.
then go to Edit > Project Settings > Physics 2D make sure there are gravity amount set!
Remember that your objects must have Rigidbody2D to be affectted by gravity.

You don’t have to apply force down since there is gravity to do this for you.

then your project should work fine. You just have to change the jump variable to a higher value as you please.
let me know if that work.

Doesn’t work still having this problem :frowning:

Sorry. I don’t have anymore ideas.
It work for me with a brand new project using only addforce up.