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 tomtomssi · Jan 23, 2014 at 01:59 PM · 2djumpjumpinglayerslinecast

How to make a 2D jumping script using Linecast?

I'm starting out with Unity and I'm stuck with making a jumping script. The problem is that the ground check fails and by pressing the jump button, my character just continues to go up.

 using UnityEngine;
 using System.Collections;
 
 public class Jump : MonoBehaviour {
     Vector2 myPos;
     Vector2 groundCheckPos;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         myPos = new Vector2 (transform.position.x, transform.position.y);
         groundCheckPos = new Vector2 (transform.position.x, transform.position.y);
         if (Input.GetKey ("w") && !isGrounded()) {
             transform.position += Vector3.up * 20 * Time.deltaTime;
                 }
     }
 
     public bool isGrounded(){
 
         return Physics2D.Linecast(myPos , groundCheckPos , 1 << LayerMask.NameToLayer("Ground"));
         }
 }

I have added both a layer and a tag called "Ground" to the objects Linecast should work on. What is wrong with the code?

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
1
Best Answer

Answer by KellyThomas · Jan 23, 2014 at 02:16 PM

 public bool isGrounded(){
     bool result =  Physics2D.Linecast(myPos , groundCheckPos , 1 << LayerMask.NameToLayer("Ground"));
     if (result) {
         Debug.DrawLine(myPos , groundCheckPos , Color.green, 0.5f, false);
     }
     else {
         Debug.DrawLine(myPos , groundCheckPos , Color.red, 0.5f, false);
     }
     return result;
 }

Try using this code for your isGrounded() method. It will draw a debug line that will let you confirm whether groundCheckPos is in the right spot.

UPDATE:

  1. myPos, and groundCheckPos are currently both being assigned the same values, this means your linecast is of zero length.

  2. Input.GetKey() will return true on each frame the key is held, you may prefer Input.GetKeyDown().

  3. No gravity is applied in this code, are you using a rigidbody for gravity?

Comment
Add comment · Show 2 · 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 tomtomssi · Jan 30, 2014 at 03:55 PM 1
Share
  1. groundCheckPos is supposed to refer to an empty game object attached to the player. How do I get its position in the script?

  2. I'm using a rigidbody2D

avatar image tomtomssi · Jan 30, 2014 at 04:30 PM 0
Share

Got it to work. Thank you

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

19 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

Related Questions

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

Advice needed on my first c# 2D jump script 1 Answer

Im trying to make it so it limits jumps in my 2d game. But when I play it, It only jumps once then your not able to jump again. Anyhelp? I need a answer fast because this is for the blackthornprod game jam 2 Answers

Player wont jump (visual script),2d player does not jump (visual script) 1 Answer

Unity 2D Top Down Jump 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