Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dokisoft · Apr 28, 2017 at 05:47 AM · unity 2dcollision detectioncollision2dlinecast

Linecast Detecting Initial Collision Correctly, Need it to Stay Until Line Doesn't Intersect

Currently have a system in place where the player has a linecast starting from it's position plus an offset (so it doesn't detect the player) and an end position. Upon line intersection the initial collision is detected, though in subsequent frame no longer identified as an active collision, and therefore ignored, until a separate movement/collision is caused.

As I'd like the Player to be able to interact with any collider that's within the linecast, I really need the collision detection to remain detecting until the player moves the object outside the line's range, so that a Player can continue pressing a button and having the parallel object respond, even when it's not the initial collision.

 void FixedUpdate() {

         // enum
         Buttons direction = playerState.getDirection ();
 
         // cast a line in the direction of the player, if object touches the line, load object into interactionInfo
         RaycastHit2D interactionInfo = Physics2D.Linecast(UsefulFunctions.position2d (transform) + UsefulFunctions.vectorAdd (direction, 16),
             UsefulFunctions.position2d (transform) + UsefulFunctions.vectorAdd (direction, 50));
 
         if(interactionInfo.collider != null) {
 
             // if a button is pressed, check that it isn't None
             if (activeButton != Buttons.None) {
     
                 // identify the mapObject and execute the command attached to the button
                 interactionInfo.collider.gameObject.GetComponent<MapObject>().Execute(activeButton);
             }
         }
     }

The main issue is Linecast() only detecting upon initial collision. I've also tried the implementation above in Update() - same effect, and utilized LineCastNonAlloc(), which kept detecting collision, though continued to do so upon leaving object, which isn't my intent either. Also set the player's RigidBody2D Collision Detection to Continuous though no luck.

Any and all help / advice would be significantly appreciated. Thanks!

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 dokisoft · Apr 28, 2017 at 06:16 PM

Got it working through the addition of isTouching(), as can be seen below

  if (interactionInfo.collider != null) {
  
      _collider = interactionInfo.collider;
  }
  
   if(_collider != null && _collider.IsTouching(thisGameObjCollider)) { ... }

Accepting this as answer as it works as desired. If however anyone has a better implementation, please don't hesitate to share :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnCollisionEnter2D is not working when an object tagged "Enemy" is not present in the game. 0 Answers

Unity2D: How to make my player not walk through walls? 1 Answer

Collider Is Touching Layers is not working... 1 Answer

multiple objects on oncollissionstay2d 1 Answer

Rigidbody gets stuck in wall after falling 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