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 GarrickWinter · Jan 16, 2014 at 05:14 AM · 2dphysics2djumpinglinecast

Checking if grounded with Linecast not working (2D)

So I am trying to make a dead-simple platformer. Here is the entirety of the script I have right now:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
     //An empty child of the PlayerController with only a position
     private Transform GroundCheck;
 
     //Use this for initialization
     void Start() 
     {
         //Find the GroundCheck
         GroundCheck = transform.Find("GroundCheck");
     }
     
     //Update is called once per frame
     void Update() 
     {
         //Check whether or not the player is grounded
         bool IsGrounded = Physics2D.Linecast(transform.position, 
                                              GroundCheck.position,
                                              0);
 
         //Start by adding jumping!
         if (Input.GetButton("Jump") &&
             IsGrounded == true)
         {
             Debug.Log("JUMP");
             rigidbody2D.AddForce(new Vector2(0, 1) * 100);
         }
     }
 }
 

Unfortunately, no dice. The terrain has a CollisionBox and is on layer 0, and the player has a rigidbody2D and is on Layer 1. If I cast without specifying a layer, or specify layer 1, the player jumps infinitely, as Linecast always returns true. If I cast specifying layer 0, which is where I expect the terrain to be, nothing ever happens and the player never jumps.

I made sure that the pivot of the 2D character is at its Bottom and not its center, so presumably the Linecast shouldn't be detecting the character itself, right? And why is it not detecting the terrain at all? I don't understand what's going on.

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

Answer by fafase · Jan 16, 2014 at 05:59 AM

It could be a wrong use of linecast. I also get quite confused on what I should get and how to use it.

Here is an example taken from http://unitygems.com/basic-ai-character/#Sight:

 Transform GroundCheck;
 int layerMask = 1 << 8;
  
 void Start(){
     GroundCheck = transform.Find("GroundCheck");
     layerMask = ~layerMask;
 }
  
 void Update(){  
     Debug.DrawLine (_transform.position, GroundCheck.position, Color.yellow);
     if (Physics.Linecast (_eyes.position, GroundCheck.position, layerMask)) {
            // Grounded
     }
 }

 
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 GarrickWinter · Jan 16, 2014 at 04:44 PM 0
Share

I don't feel I know what's going on with layer masks there. Why use 1 and 8? Where should I look to find what numbers I should use in my game? EDIT: Never$$anonymous$$d, I think I've figured it out. It seems to work now, except that it occasionally registers a double-jump ins$$anonymous$$d of a single-jump, even with the tiniest possible length of Linecast.

avatar image fafase · Jan 18, 2014 at 12:22 PM 0
Share

This 1<<8 thing is bit manipulation. There are more straight forward to do that but we'll...it just means you set bit number 8 with a 1.

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

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

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

FixedUpdate and LineCast? 0 Answers

Colliders 2d apparently touch each other even if they should not. 2 Answers

Set targetjoint2d anchor position at mouse position in 2d 0 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