Problems with jumping in Doodle Jump like game
Hi, guys. I'm a beginner to Unity, and I'm trying to create a Doodle Jump-like game, with many differences of course. I'm facing 2 issues, however.
The first is that the upward jump is very sudden, and I want it to be more natural.
The second is that when the level begins, I want the player sprite to automatically jump upwards from the bottom of the screen. I have added an AddForce in the start method, but the sprite simply falls downwards.
I'm stumped, so please suggest ways I can make this work.
Here is my code - note that it is incomplete.
public class Player : MonoBehaviour {
Vector2 jumper = new Vector2(0, 2000);
public static bool dead = false;
public float speed;
public bool onPlat = false;
void Start ()
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 1000),ForceMode2D.Force);
}
private void FixedUpdate()
{
if(dead==false)
{
Vector2 movement = new Vector2(Input.acceleration.x, -5);
GetComponent<Rigidbody2D>().velocity = movement * speed;
if (onPlat == true)
{
GetComponent<Rigidbody2D>().AddForce(jumper*Time.fixedDeltaTime, ForceMode2D.Impulse);
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
onPlat = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
onPlat = false;
}
}
Answer by RecyclingBen · Mar 22, 2017 at 12:05 PM
Instead of the initial jump being AddForce()
try using velocity like the way you do later in the script
To answer the other question, setting Vector2.velocity()
equal to something means it will have no transition from the speed it is going down to the speed it is going up (kind of like a flappy bird effect). AddForce will give you a more natural effect, but it also means that depending on how far you jumped up it will effect the speed you jump up again. Most arcade games use velocity and most 3D games that are meant to have realistic physics would use AddForce()
Answer by Kanteron · Mar 22, 2017 at 01:54 PM
@RecyclingBen As I'm creating a 2D Doodle jump-like game, which method do you suggest I use for the jumping? Also, I tried using velocity for the initial jump, but with no success. Both AddForce and velocity work if I remove the code for the jumping, but don't otherwise.