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
1
Question by SpecularDreams · Dec 24, 2013 at 04:26 AM · javascript

rigidbody2D only jump while grounded

Hello I am a new user to Unity and I am currently using JavaScript. Right now i have a question on how to make it that my rigid body can only jump while being grounded. Right now you can press W over and over and go flying up into the sky. I am using the code

pragma strict

var jumpSpeed: float = 10;

function Start () {

} function Update () { if (Input.GetKeyDown (KeyCode.W)){

  rigidbody2D.velocity.y = jumpSpeed;
 } 

}

Any help would be greatly appreciated! The ground i am using is completely flat!

Comment
Add comment · Show 1
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
avatar image robertbu · Dec 24, 2013 at 04:31 AM 0
Share

In the future use the 101/010 button to format your code. The quotation button does not work well for code.

You'll get a better answer for your question if you describe your ground. There are several ways to detect if you are grounded...some specific 'y' value, raycasting down and checking the distance, detecting a collision with the ground to name three.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hirenkacha · Dec 24, 2013 at 04:28 AM

If you want the character to move and jump on inputs, you can use CharacterController.

Following commented code is right for you.

 var groundCheck : Transform;
 var b_grounded : bool = false;
 function Start ()
 {
    groundCheck = transform.Find("groundCheck");
 }
  
 // Update is called once per frame
 function Update () {
 b_grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
  
 print(b_grounded);
 }
 }
Comment
Add comment · Show 7 · 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
avatar image Santosh Patil · Dec 24, 2013 at 05:16 AM 1
Share

I'm not familiar with javaScript but i've c# code which be can used to detect character is grounded or not,

 using UnityEngine;
 using System.Collections;
 
 public class GroundTest: $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     private Transform groundCheck;
     bool b_grounded = false;
     void Start () 
     {
         groundCheck = transform.Find("groundCheck");
     }
     
     // Update is called once per frame
     void Update () {
         b_grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << Layer$$anonymous$$ask.NameToLayer("Ground"));
 
         print(b_grounded);
     }
 }
avatar image SpecularDreams · Dec 24, 2013 at 05:59 AM 0
Share

Hey hirenkicha I cant use Character Controller because I am using the new Unity 2D features. Is the script still viable to use?

avatar image hirenkacha · Dec 24, 2013 at 06:33 AM 0
Share

yup you can use this.

avatar image hirenkacha · Dec 24, 2013 at 06:44 AM 0
Share

yes.. you can use. Just add a layer to the ground sprite as "Ground" and a gameobject named "groundCheck" at the feet of the character which will check for the ground layer

avatar image SpecularDreams · Dec 24, 2013 at 07:55 AM 0
Share

Thank you! Now I just need to fix my darn $$anonymous$$ono Develop, sorry for being slow, I literally just joined this morning:)

Show more comments

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

20 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

Related Questions

Spawn object in random areas 2 Answers

Parenting Objects 1 Answer

What is #pragma strict meaning? 2 Answers

Code compilation error 1 Answer

EMERGENCY!!! PLEASE HELP!!!!! 3 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