- Home /
How do I make my player jump? Unity 2019,How do I make my player jump?
How do I make my player Jump? All it does is float when I press W or the UP key; and yes, I've added collisions and gravity. If the W or up key isn't pressed, the player will fall back down onto the surface of the tilemap; but how do I make it so that the player can jump and not just float? Please keep in mind that I'm brand new to Unity and that I'm also using 2019. Here is my script currently:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 5f;
public float hitPoints = 100f;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
if (rb == null)
{
Debug.LogError("Player::Start cant find RigidBody2D </sadface>");
}
// Update is called once per frame
void Update()
{
}
}
// this is called at a fixed interval for use with physics objects like the RigidBody2D
void FixedUpdate()
{
// check if user has pressed some input keys
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
// convert user input into world movement
float horizontalMovement = Input.GetAxisRaw("Horizontal") * moveSpeed;
float verticalMovement = Input.GetAxisRaw("Vertical") * moveSpeed;
//assign world movements to a Veoctor2
Vector2 directionOfMovement = new Vector2(horizontalMovement, verticalMovement);
// apply movement to player's transform
rb.AddForce(directionOfMovement);
}
}
}
,I'm using Unity 2019 and I've taken a tutorial on simple movement since then I've made a top-down with gravity and physics, which is basically just a normal platformer.. ACCEPT!... The player's jump isn't implemented. To clarify, what I mean is that when the D or up key is pressed, the player takes a bit to gain momentum, but then goes flying. If anyone has the answer to how to fix this, please keep in mind that my goal is to make a Metroidvania (2D, basically a platformer, but more complex.), and I'm relatively new to Unity, so I have little to no idea what I'm doing.
Here is the script that I'm using currently:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 5f;
public float hitPoints = 100f;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
if (rb == null)
{
Debug.LogError("Player::Start cant find RigidBody2D </sadface>");
}
// Update is called once per frame
void Update()
{
}
}
// this is called at a fixed interval for use with physics objects like the RigidBody2D
void FixedUpdate()
{
// check if user has pressed some input keys
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
// convert user input into world movement
float horizontalMovement = Input.GetAxisRaw("Horizontal") * moveSpeed;
float verticalMovement = Input.GetAxisRaw("Vertical") * moveSpeed;
//assign world movements to a Veoctor2
Vector2 directionOfMovement = new Vector2(horizontalMovement, verticalMovement);
// apply movement to player's transform
rb.AddForce(directionOfMovement);
}
}
}
Answer by Le-Capitaine · Jan 24, 2020 at 02:32 PM
What I usually do for platformer movement is a raycast the length of the downwards velocity, clamped between a minimum (from which you'll check ground when grounded) and a maximum (so the game doesn't think you've landed before touching the ground). If the ray hits, you're grounded, if not you're airborne and can't jump.
Your answer
Follow this Question
Related Questions
Convert recorded points to jump movement 1 Answer
How do I stop jumping in mid air? 3 Answers
Lag Caused by Overlapping 2D Colliders 1 Answer
Model stuck on collider 1 Answer
2D Raycast not working as intended 2 Answers