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 dustingunn · Feb 28, 2015 at 06:16 AM · collisionphysicsraycastingraycasthit2d

Problem with custom raycast2D collision detection system.

I created a custom moving and colliding system, and it mostly works, but with slow-moving objects I've noticed they can get stuck on eachother and start moving in weird slow arcs through walls and anything else. I'm thinking this is probably due to the skinWidth attempts; I just guessed how to implement skin width to stop objects getting stuck in every collision, and it worked until now. I'm not sure if I'm doing the repositioning based on the centroid right, or adding/subtracting wrong. Here's the collision and moving systems:

 void LateUpdate () {
         // Add additive velocity then drag it
         var _velocity = velocity;
         
         _velocity += additiveVelocity * (Time.deltaTime * 60f); // Additional velocity is always multiplied by delta.
 
         additiveVelocity /= 1f + (drag * Time.deltaTime);
         if (additiveVelocity.magnitude < 0.05f) { additiveVelocity = Vector2.zero; }
 
         if (_velocity.magnitude > 0.01f)
             UpdateMovement(ref _velocity, wallSlide);
 
         // If sliding is on, and _velocity didn't return zero, do one more pass
         if (wallSlide && _velocity.magnitude > 0.001f)
         {
             //print("2nd vel: " + _velocity);
             UpdateMovement(ref _velocity, false);
         }
         //else if(wallSlide) print(name+" no velocity! " + _velocity*100f);
         
         velocity = Vector2.zero;
     }
 
     void UpdateMovement( ref Vector2 _velocity, bool _testSlide)
     {
         RaycastHit2D hit = new RaycastHit2D();
         switch (colliderType)       // Cast either a box or a circle
         {
             case ColliderType.Circle:
                 hit = CastCircle(this, _velocity);
                 break;
             case ColliderType.Box:
                 hit = Physics2D.BoxCast(myTransform.position, size - new Vector2(skinWidth, skinWidth), 0, new Vector2(_velocity.x, 0.0f), _velocity.magnitude, mask);
                 break;
         }
 
         // No hit, Move object:
 
         if (hit.collider == null || Physics2D.GetIgnoreCollision(this.collider2D, hit.collider))
         {
             myTransform.position += new Vector3(_velocity.x, _velocity.y);
             _velocity = Vector2.zero;
             //print(name + " hi");
         }
 
         // Hit object, settle collisions:
 
         else
         {
             
 
             // Grab BaseObject and PhysicalObject from the hit object.
             BaseObject _obj = hit.collider.GetComponent<BaseObject>();
             PhysicalObject _phys = null;
             bool _isTrigger = false;
             if (_obj != null)
                 _phys = _obj.GetComponent<PhysicalObject>();
 
             // Get if object hit is a trigger:
             if (_phys != null)
                 _isTrigger = _phys.trigger;
 
             // Move to centroid
             myTransform.position = new Vector3(hit.centroid.x, hit.centroid.y, 0f);
             // Retract movement by skinWidth so it stops colliding
             var _skin = _velocity.normalized * Mathf.Min(skinWidth, _velocity.magnitude);
             myTransform.position -= new Vector3(_skin.x, _skin.y);
 
             // Collided with something, call the delegate of the host IF NOT TRIGGER.
             if (onCollide != null && !_isTrigger)
             {
                 //print("no trigger for " + this.name + " and " + hit.collider.name);
                 onCollide(hit, _velocity);
             }
 
             // Try to call the collision for the other object.
             if (_phys != null)
             {
                 //if (this.gameObject.name == "Ball")
                 Vector2 returnSource = hit.point - hit.normal;
                 Vector2 returnDir = hit.normal * 2f; //hit.point - new Vector2(_obj.transform.position.x, _obj.transform.position.y);
                 RaycastHit2D returnHit = CastRayToTarget(returnSource, this.gameObject, returnDir * 2f);
 
                 if (returnHit.collider != null)
                 {
                     if (!_isTrigger)
                         _obj.OnCollide(returnHit, _velocity);   // Call collision on the other object, if it's not a trigger.
                     else
                         _obj.OnTriggerIn(returnHit, _velocity); // Call OnTriggerIn on the trigger object
                 }
             }
 
             // If wallSlide is on, subtract the collision normal*speed from velocity for a 2nd pass
             if (wallSlide && _testSlide)
             {
                 Vector3 perpVec = Vector3.Cross(hit.normal, Vector3.forward);
 
                 _velocity = Vector2.Dot(velocity, (Vector2)perpVec) * perpVec.normalized;
             }
         }
     }
 
     // RayCast a circle
     public static RaycastHit2D CastCircle(PhysicalObject obj, Vector2 _velocity)
     {
         RaycastHit2D hit = Physics2D.CircleCast(obj.transform.position, obj.radius - obj.skinWidth, _velocity, _velocity.magnitude, obj.mask);
         //if (hit.collider != null)
             //Debug.DrawLine(obj.transform.position, hit.point, Color.green, 5f);
         return hit;
     }
 
     // RayCast a box
     public static RaycastHit2D CastBox(PhysicalObject obj, Vector2 _velocity)
     {
         RaycastHit2D hit = Physics2D.BoxCast(obj.transform.position, obj.size - new Vector2(obj.skinWidth, obj.skinWidth), 0, _velocity, _velocity.magnitude, obj.mask);
         return hit;
     }
 
     // RayCast a ray
     public static RaycastHit2D CastRay(PhysicalObject obj, Vector2 _velocity)
     {
         return CastRay((Vector2)obj.transform.position, _velocity, obj.mask);
     }
     public static RaycastHit2D CastRay(Vector2 _start, Vector2 _velocity, LayerMask _mask)
     {
         RaycastHit2D hit = Physics2D.Raycast((Vector3)_start, _velocity, _velocity.magnitude, _mask);
         Debug.DrawLine((Vector3)_start, (Vector3)_start + new Vector3(_velocity.x, _velocity.y), Color.red, 5f);
         return hit;
     }
 
     // Cast ray to specific object
     public static RaycastHit2D CastRayToTarget(Vector2 _start, GameObject _tar, Vector2 _velocity)
     {
         int _layerCache = _tar.layer;
         _tar.layer = 11;
         RaycastHit2D _hit = CastRay(_start, _velocity, LayerMaskHelper.OnlyIncluding(11));
         _tar.layer = _layerCache;
         return _hit;
     }
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

Answer by siaran · Feb 28, 2015 at 12:52 PM

Since you say the problem happens with slow-moving objects, I suspect your problem is here:

  // Move to centroid
 myTransform.position = new Vector3(hit.centroid.x, hit.centroid.y, 0f);
 // Retract movement by skinWidth so it stops colliding
 var _skin = _velocity.normalized * Mathf.Min(skinWidth, _velocity.magnitude);
 myTransform.position -= new Vector3(_skin.x, _skin.y);

Specifically, the part where you take

 Mathf.Min(skinWidth, _velocity.magnitude);

On a slow moving object, your _velocity.magnitude may very well be lower than your skinWidth, and, if I'm reading your code right, that would cause it to not move back far enough. (Why are you taking the minimum, anyway? Wouldn't you want it to always just be skinWidth then?)

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 dustingunn · Feb 28, 2015 at 06:14 PM 0
Share

I added that after I was getting problems with them moving backwards when getting stuck together, because the amount moved was less than the skin width. It helped it somewhat but wasn't a perfect solution.

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

Direction/Hit Detection without triggers. 1 Answer

Using Raycasts to change the X and Y position of a moving GameObject? 1 Answer

My Raycasts seem to sometimes miss 0 Answers

Collision normals changing based on player position 0 Answers

Collision stuck issue 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