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 andrew_196 · Mar 20, 2014 at 02:23 PM · c#2dlinecastgrounded

unity2d issue with linecasts.

Hello I am having a problem where the linecast will only detect a collision to a floor in the middle of the object but in my game there are mutliple floating objects and to get the optimin jump you need to jump at the end of the block. This is described in the picture below.

my code for this is:

     private Transform groundDetector;
     
         void Awake()
         {
             groundDetector = transform.Find("GroundCheck");
         }
     
     void Update()
         {
             grounded = Physics2D.Linecast(transform.position, groundDetector.position, 1 << LayerMask.NameToLayer("Terrain"));  
             
 if (grounded) {
             doubleJump = false;
         }
         
         if ((grounded || !doubleJump) && jump) {
             // Add a vertical force to the player.
             anim.SetBool("Ground", false);
 
             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
 
             rigidbody2D.AddForce(new Vector2(0f, jumpForce));
 
             if(!grounded && !doubleJump){
                 doubleJump = true;
             }
         }
 
         }


for some reason the jump actually works but after the mid-point thedouble jump does not work

alt text

desc.png (12.8 kB)
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
0
Best Answer

Answer by Razouille · Mar 20, 2014 at 04:11 PM

A simple way is to create 3 small boxes which will all fire a Linecast : one on the middle, one on the left and one on the right of the player, then you'll have to check if all hits are null in order to avoid your problem.

A second solution is to use the OverlapArea

With your current script a third solution will be to create 3 ground detectors and do a LineCast to each of them. You can find an example here

Comment
Add comment · Show 4 · 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 andrew_196 · Mar 20, 2014 at 04:28 PM 0
Share

sorry your link seems to be broken. its a user file

avatar image andrew_196 · Mar 20, 2014 at 04:30 PM 0
Share

sorry I found the link but it doesn't show much information. So if you could expand on how to use it.

and one thing on the line cast on the left and right how would you get this script to detect the collision so I could initiate the jump.

avatar image Razouille · Mar 20, 2014 at 05:19 PM 0
Share

I added a third solution to my post, it's better than use an OverlapArea

avatar image andrew_196 · Mar 20, 2014 at 07:15 PM 0
Share

thanks that is great.

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

21 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

Related Questions

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

Why do I need bitwise shift operator in linecast? 1 Answer

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

issues with jumping and linecast2d and raycast2d 0 Answers

Assigning found enemy to target variable 2 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