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 Jafke · Apr 27, 2014 at 08:00 PM · 2djumpinglinecastgrounded2dplatformer

How to check for the ground in Unity 2D for Jumping

So I have this script where I use a timer to prevent jumping, but this is sort of overrided if you jump at a high block and have enough fall time to get the timer to zero, also it is weird for me if I have to wait a second or two even if I'm on the ground and needing to jump. What I want to do is have something check for the ground, and then set a variable to true if im on the ground. If that variable is true, then I can jump. Also I have heard of linecasting, but I don't understand how it works, so if that is your solution can you tell me what the code does along with how linecasting works, because I don't get the Unity description of it.

Here is the script I have right now called "Movement Test":

 #pragma strict
 
 var movementSpeed : float = 5.0f;
 var jump : float = 0;
 var jumpSpeed : float = 5;
 var jumpTimer : float = 0;
 
 function Start () {
 
 }
 
 function Update () {
 if (jump == 1){
     jumpTimer = jumpTimer + 1;    
     if (jumpTimer >= 85) {
         jumpTimer = 0;
         jump = 0;
     }
 }    
 
 
 // This is how the keys work
 if(Input.GetKey("a") || Input.GetKey("left")){
     transform.position -= Vector3.right * movementSpeed * Time.deltaTime;
     } else if(Input.GetKey("d") || Input.GetKey("right")){
         transform.position += Vector3.right * movementSpeed * Time.deltaTime;
     }
 if(Input.GetButtonDown("Jump") || Input.GetKey("w")){
         if (jump==0) {
         rigidbody2D.velocity.y = jumpSpeed;
         jump = 1;
     } 
 }
 }
 
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 falconer · Nov 15, 2014 at 07:51 PM 0
Share

Here is an article I found which explains how to use Physics.OverlapCircle to check if object is grounded. $$anonymous$$aybe it might help someone who is struggling to get this

Check Object Grounded

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vandarthul · Apr 28, 2014 at 03:56 AM

You can use Physics.Linecast or Raycast to control if you are touching the ground or not. You can check the unity's 2D tutorial and get the idea of using Linecast. There is a script written in that tutorial exactly what you want.

http://pastebin.com/JS3Ktbgp Here is a script that I wrote long time ago that suits your needs + it has double jump too, but be aware of that this is for 3D, you might need to convert it for 2D(Rigidbody2D instead of Rigidbody for example). Also, that this script is old and I wasn't aware of many things back then. But still, you can comb out the areas that you need and/or you can get the idea behind it.

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

22 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

Related Questions

Check if 2D Player is grounded with Physics2D.Linecast 1 Answer

How to make a 2D jumping script using Linecast? 1 Answer

issues with jumping and linecast2d and raycast2d 0 Answers

Checking if grounded with Linecast not working (2D) 1 Answer

unity2d issue with linecasts. 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