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 LTonon · Jul 10, 2017 at 10:48 PM · programmingcollision detection2d-physicsrigidbody physicsrigidbody collision

Player's collider doesn't touch obstacle's collider every time that it collides!

I've been trying to expand Unity's 2D Platformer Character Controller Tutorial, since I wanted to find a way of dealing with collisions between game objects without having to rely on OnCollision2D functions because I don't want game objects interacting between each other using physics. Therefore, I made a script where I give a value to a kinematic Rigidbody2D's velocity on the FixedUpdate function, then I check if a collision has happened. So I ended up with this function for the collision detection part:

 public void CheckCollisionPlayer() {
         distance = (rb.velocity * Time.deltaTime).magnitude;
 
 
         // Checks if collision exists only if collision distance if bigger than the defined minimum
         if (distance > minMoveDistance) {
             count = rb.Cast (rb.velocity, contactFilter, hitBuffer, distance);
             hitBufferList.Clear ();
 
 
             for (int i = 0; i < count; i++) {
                 hitBufferList.Add (hitBuffer [i]);
             }
 
             // For all of the results of the Cast...
             for(int i = 0; i < hitBufferList.Count; i++) {
 
                 // Transform position will stay the same as if it has not moved with the velocity.
                 transform.position = (Vector2)transform.position - (rb.velocity * Time.deltaTime);
     
             }
         }
     }

I wanted to create a movement like of a space ship in 2D, where the player is going from the left to the right and could move vertically, being also able to collide with objects by using this function. Unfortunately, after testing it I found out that this is making my player Game Object stop before it collides in fact, like as if there is a gap between he and the other object, and I'm confused because when I change the player direction, as he is "colliding", it briefly touches the other collider before it begins to go in that direction.

Can someone please help me? I'm lost!

Comment
Add comment · Show 2
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 Cornelis-de-Jager · Jul 11, 2017 at 12:48 AM 1
Share

Try changing the collision type to continues ins$$anonymous$$d of discrete.

avatar image LTonon Cornelis-de-Jager · Jul 11, 2017 at 01:05 AM 0
Share

I just tried and... Sadly, I got the same result, but thanks for the idea hahah

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JustinEllis · Jul 11, 2017 at 03:45 PM

I have the exact same problem. It has to do with moving objects using the transform of the object. Try using physics based movement and you should get better results. That or you need to stop trying to move the transform while its still touching the object. Its kind of a havk and it only makes a slight difference

Comment
Add comment · Show 1 · 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 LTonon · Jul 11, 2017 at 05:44 PM 0
Share

Yeah, I guess my previous way of dealing with movement wasn't that good to begin with... But fortunately all I wanted to do was to specify exactly with what Game Objects my collider would check for collision, and I just found a way to do that! Using the Collision Layer $$anonymous$$atrix I can select the Layers (in my case, "Enemy" and "EnemyBullet") that my Game Object Layer ("Player") can collide, and still be able to use the OnCollision2D functions, making my movement much more precise (at least it looks so) and, I hope, more efficient!

But as I was curious, I tried to make the rigidbody's velocity equals to zero ins$$anonymous$$d of changing the transform value, and it looks like it works! I just had to do some $$anonymous$$or improvements on the section of FixedUpdate where I give a value to velocity to test before if the object was still colliding and trying to go in the same direction as before. I will stay with the OnCollision approach since it seems more intuitive and simple, but thanks you so much for your answers! It helped me a lot in problems that may occur in future projects.

avatar image
1

Answer by springwater · Jul 11, 2017 at 05:51 AM

Try scaling down your colliders, they have controls for that in the inspector. Or scale up the graphics independently.

Comment
Add comment · Show 1 · 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 LTonon · Jul 11, 2017 at 02:06 PM 0
Share

I tried it out but didn't work either... I guess I have a problem with my way of "stopping" the Game Object, because I'm just changing the transform.position value, even though the velocity is still acting, so... $$anonymous$$y guess is that it has to do with this, but thanks for the help!

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

75 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 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 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 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

Multiple Cars not working 1 Answer

Using OnTriggerEnter2D/OnTriggerExit2D with Time.timeScale set to 0 1 Answer

How can i make my RigidBodyFPSController to move when Is Kinematic is true ? 1 Answer

How can i get harmed and healed by diferent game objects 0 Answers

Lode Runner: Walk Over a Hole with Enemy 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