- Home /
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;
}
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?)
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
![](https://koobas.hobune.stream/wayback/20220613184553im_/https://answers.unity.com/themes/thub/images/avi.jpg)