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 Bslashingu · Nov 22, 2013 at 02:27 AM · 2draycastlinecast

4.3 2D Line/Ray Cast Hit Layermasks not working

I am attempting to make a custom 2D CharacterController in Unity 4.3 and I am trying to make an IsGrounded function but it's registering raycasts and linecasts on only the player. Here is my code, simple enough.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(Rigidbody2D))]
 
 public class CharacterMovement : MonoBehaviour {
 
     public LayerMask ground;
     
     float PlayerHeight;
     
     void Start () {        
         PlayerHeight = GetComponent<BoxCollider2D>().size.y;    
     }
 
     // Use this for initialization
     void Update () {
 
         print (IsGrounded());
 
     }
 
     public bool IsGrounded () {
         bool hit = Physics2D.Linecast(Flatten(transform.position), new Vector2(transform.position.x,transform.position.y - (PlayerHeight * -.5f)), 1 << ground.value);
         return hit;
     }
 
 }

As a note, I am using 2DToolkit for my spritework and I removed any code (potentially) irrelevant to the problem.

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 nngafook · Nov 24, 2013 at 06:58 AM 0
Share

Have you by chance figured this out? I'm facing the same problem right now. Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chaos119 · Nov 30, 2013 at 03:43 PM

I downloaded the demo that unity used to show their new 2D support features to find this issue myself. Here is what I found and it works fine :)

 private Transform groundCheck;    // A position marking where to check if the player is grounded.
 private bool grounded = false; //Whether or not the player is grounded.
 
 void Update()
     {
     // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
     grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

}

All you need to do is assign create a Empty GameObject, name it groundcheck(or whatever you see fit). Then make it a child of the player object and place it below the player.

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 emisael-carrera · May 05, 2014 at 08:36 PM 0
Share

Thank you, It worked for me... I was getting insane x_x

avatar image dpw · May 19, 2014 at 09:01 PM 0
Share

what if its a cube? How will I check all its sides? (IN 2D)

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

issues with jumping and linecast2d and raycast2d 0 Answers

Raycast Physics2D not working as expected 1 Answer

Linecast, rotate end point relative to objects rotation 1 Answer

Getting a Raycast rope to follow its object 0 Answers

onGround raycasts return onGround is true while colliding with platform sides 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