Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 skagontale · Sep 16, 2021 at 11:40 PM · 2d-platformerjumpjumpingnot workingjumping object

2d rigidbody jump not working

i am trying to make a platformer but my jump script is not working: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class player_move : MonoBehaviour
 {
     [SerializeField] private LayerMask platformLayerMask;
     public bool canThrow = true;
     public Transform throwPlat;
     public bool hasJumped = false;
     public bool hasJumpedOnPlat = false;
     public float Dir;
     public float speed = 5f;
     private Rigidbody2D rb;
     private BoxCollider2D boxCollider2d;
     public float jumpSpeed = 30f;
     public float JumpTime;
 
     void Start()
     {
         jumpSpeed = 50f;
         rb = GetComponent<Rigidbody2D>();
         boxCollider2d = GetComponent<BoxCollider2D>();
     }
 
     void Update()
     {
         Dir = Input.GetAxisRaw("Horizontal");
 
         if (Input.GetKeyDown("space") && canThrow)
         {
             canThrow = false;
             ThrowPlatThrown();
         }
 
         if (isGrounded() && Input.GetKeyDown("w"))
         {
             jumpSpeed = 100f;
             Debug.Log("start jump");
             rb.velocity = Vector2.up * jumpSpeed;
         }
     }
 
     public void ThrowPlatThrown()
     {
         float waitTime = 10f;
         while (waitTime > 0f || hasJumped)
         {
             if (hasJumpedOnPlat)
             {
                 hasJumpedOnPlat = false;
                 hasJumped = true;
             }
             waitTime -= Time.deltaTime;
         }
         Debug.Log("hasJumpedOnPlat");
     }
 
     private void FixedUpdate()
     {
         rb.velocity = new Vector2(Dir * speed, 0f);
     }
 
     private bool isGrounded()
     {
         float extraHeightText = .1f;
         RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, extraHeightText, platformLayerMask);
         Color rayColor;
         if (raycastHit.collider != null)
         {
             rayColor = Color.green;
         } else {
             rayColor = Color.red;
         }
         Debug.DrawRay(boxCollider2d.bounds.center - new Vector3(boxCollider2d.bounds.extents.x, boxCollider2d.bounds.extents.y + extraHeightText), Vector2.right * (boxCollider2d.bounds.extents.x), rayColor);
         //Debug.Log(raycastHit.collider);
         return raycastHit.collider != null;
     }
 }

this is all of the code but the part that isnt working is:

         if (isGrounded() && Input.GetKeyDown("w"))
         {
             jumpSpeed = 100f;
             Debug.Log("start jump");
             rb.velocity = Vector2.up * jumpSpeed;
         }

on the screen the player is not jumping but upun verry close inspection the green box around the player grows, this might just be the boxxcast being called though. i have tried increasing the force but it doesnt work. also i tried changing to an addForce jump but it just started instantly teleporting the player up. a new jump script would work fine i just need it to be able to be called in random circumstances (through coroutines, not functions) but any help is good. gravity on rigidBody is 35, rotation is disabled, this is copied code, and i am on the newest version of unity.

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 Chimz · Sep 25, 2021 at 08:47 PM

I know you've tried addForce, but have you tried it in Impulse mode? Try this:

(jumpForce is your jumpSpeed)

 rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
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

141 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 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

Raycast2D not working 1 Answer

Problem with Jump 1 Answer

Jumping only once (2D) 2 Answers

Choose Start and End Frame for 2D Animation 0 Answers

I have a problem with my jump script 1 Answer


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