Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tylerjohncunningham · Jan 24, 2020 at 08:31 AM · jump2d platformer2d collision2d physics

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);
         }
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges