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 /
  • Help Room /
avatar image
0
Question by rad202k · Sep 26, 2018 at 03:00 AM · rigidbody2dcollider2d

Can someone explain how this collision detection work and why it fails sometimes?

I tried to adapt the 2d platformer character controller from this live session: https://www.youtube.com/watch?v=wGI2e3Dzk_w&list=PLX2vGYjWbI0SUWwVPCERK88Qw8hpjEGd8

Into a 2d top down character controller. It seemed to work but it is possible to move into colliders with some combination of keys pressed that I couldn't really find out, but it's easy to make it happen.

The thing is I don't understand how the collision detection is really working here so I don't know how to fix it. I appreciate if someone can explain how this works.

Thanks :)

This is how the player is set up:

alt text

PlayerControllerTopDown2D.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerControllerTopDown2D : PhysicsObject2D
 {
     public float maxSpeed = 7;
  
     private SpriteRenderer spriteRenderer;
     private Animator animator;
 
     private bool facingUp, facingDown, facingLeft, facingRight;
 
     void Awake()
     {
         spriteRenderer = GetComponent<SpriteRenderer>();
         animator = GetComponent<Animator>();
 
         facingUp = true;
         facingDown = facingLeft = facingRight = false;
     }
 
     protected override void ComputeVelocity()
     {
         Vector2 move = Vector2.zero;
 
         move.x = Input.GetAxis("Horizontal");
         move.y = Input.GetAxis("Vertical");
  
         targetVelocity = move * maxSpeed;
 
         if (move.y > minMoveDistance && !facingUp)
         {   
             clearOthersAndSet(0);
             // sprite rotation
         }
 
         if (move.y < -minMoveDistance && !facingDown)
         {
             clearOthersAndSet(1);
             // sprite rotation
         }
 
         if (move.x < -minMoveDistance && !facingLeft)
         {
             clearOthersAndSet(2);
             // sprite rotation
         }
 
         if (move.x > minMoveDistance && !facingRight)
         {
             clearOthersAndSet(3);
             // sprite rotation
         }
 
 
     }
 
     void clearOthersAndSet(int x)
     {
         switch (x)
         {
             case 0:
                 facingUp = true;
                 facingDown = facingLeft = facingRight = false;
                 break;
             case 1:
                 facingDown = true;
                 facingUp = facingLeft = facingRight = false;
                 break;
             case 2:
                 facingLeft = true;
                 facingUp = facingDown = facingRight = false;
                 break;
             case 3:
                 facingRight = true;
                 facingUp = facingDown = facingLeft = false;
                 break;
         }
     }
 }
 


PhysicsObject2D.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PhysicsObject2D : MonoBehaviour
 {
     protected Rigidbody2D rb2d;
     protected Vector2 velocity;
     protected Vector2 targetVelocity;
 
     protected ContactFilter2D contactFilter;
     protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
     protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
 
 
     protected const float minMoveDistance = 0.001f;
     protected const float shellRadius = 0.01f;
 
     protected bool hitSomething = false;
     
 
     void OnEnable()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     void Start()
     {
         contactFilter.useTriggers = false;
         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
         contactFilter.useLayerMask = true;
     }
 
     void Update()
     {
         targetVelocity = Vector2.zero;
         ComputeVelocity();
     }
 
     protected virtual void ComputeVelocity()
     {
 
     }
 
     void FixedUpdate()
     {
         if (hitSomething)
         {
             targetVelocity = -targetVelocity * 5;
             hitSomething = false;
         }
 
         velocity.x = targetVelocity.x;
         velocity.y = targetVelocity.y;
 
         Vector2 deltaPosition = velocity * Time.deltaTime;
 
         Vector2 move = Vector2.right * deltaPosition.x;
 
         Movement(move, false);
 
         move = Vector2.up * deltaPosition.y;
 
         Movement(move, true);
     }
 
     void Movement(Vector2 move, bool yMovement)
     {
         float distance = move.magnitude;
 
         if (distance > minMoveDistance)
         {
             int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
             
             if (count > 0)
                 hitSomething = true;
             else
                 hitSomething = false;
 
             hitBufferList.Clear();
             for (int i = 0; i < count; i++)
             {
                 hitBufferList.Add(hitBuffer[i]);
             }
                 
             for (int i = 0; i < hitBufferList.Count; i++)
             {
                 float modifiedDistance = hitBufferList[i].distance - shellRadius;
                 distance = modifiedDistance < distance ? modifiedDistance : distance;
             }
         }
 
         rb2d.position = rb2d.position + move.normalized * distance;
     }
 }
 
 
playerinspector.png (101.1 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

0 Replies

· Add your reply
  • Sort: 

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

155 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Stop ball sprite passes through the wall? 1 Answer

How to stop game object movement instantly after collision is detected ? 1 Answer

OnCollisionEnter 2d not working 0 Answers

objects overlap on collision 0 Answers

Is it possible to know the angle of collider2D border in touch place with RigidBody2D? 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