public void CollisionCheck() { if (security > 25) { return; } security++; Vector3 pointUp = Position.position + (CapsuleCollider.center + Vector3.up * (CapsuleCollider.height / 2 - CapsuleCollider.radius)); Vector3 pointDown = Position.position + (CapsuleCollider.center + Vector3.down * (CapsuleCollider.height / 2 - CapsuleCollider.radius)); if (!Physics.CapsuleCast(pointUp, pointDown, CapsuleCollider.radius, AIagent.velocity.normalized, out capsuleRaycast, Mathf.Infinity, CollisionMask) || AIagent.velocity.magnitude < 0.001f) { return; } float snapdistance = capsuleRaycast.distance + skinWidth / Vector3.Dot(AIagent.velocity.normalized, capsuleRaycast.normal); if (snapdistance < AIagent.velocity.magnitude * Time.deltaTime) { if (snapdistance > 0) { Position.position += AIagent.velocity.normalized * snapdistance; } Vector3 normalForce = HelpClass.NormalizeForce(AIagent.velocity, capsuleRaycast.normal); InheritVelocity(capsuleRaycast.transform, ref normalForce); AIagent.velocity += normalForce; } CollisionCheck(); return; }
/// <summary> /// Checks for collision using <see cref="capsuleRaycast"/> and recursive calls. /// </summary> /// <param name="frameMovement"></param> private void CollisionCheck(Vector3 frameMovement) { Vector3 pos = transform.position; if (Grounded() == false) { transform.position += frameMovement; return; } Debug.DrawRay(transform.position, frameMovement.normalized, Color.red); pointUp = pos + (coll.center + Vector3.up * (coll.height / 2 - coll.radius)); pointDown = pos + (coll.center + Vector3.down * (coll.height / 2 - coll.radius)); if (Physics.CapsuleCast(pointUp, pointDown, coll.radius, frameMovement.normalized, out capsuleRaycast, Mathf.Infinity, collisionMask)) { float angle = (Vector3.Angle(capsuleRaycast.normal, frameMovement.normalized) - 90) * Mathf.Deg2Rad; float snapDistanceFromHit = skinWidth / Mathf.Sin(angle); Vector3 snapMovementVector = frameMovement.normalized * (capsuleRaycast.distance - snapDistanceFromHit); snapMovementVector = Vector3.ClampMagnitude(snapMovementVector, frameMovement.magnitude); frameMovement -= snapMovementVector; Vector3 frameMovementNormalForce = HelpClass.NormalizeForce(frameMovement, capsuleRaycast.normal); frameMovement += frameMovementNormalForce; transform.position += snapMovementVector; if (frameMovementNormalForce.magnitude > 0.001f) { Vector3 velocityNormalForce = HelpClass.NormalizeForce(velocity, capsuleRaycast.normal); velocity += velocityNormalForce; } if (frameMovement.magnitude > 0.001f) { CollisionCheck(frameMovement); } return; } else { transform.position += frameMovement; } }
/// <summary> /// Checks for collision using <see cref="capsuleRaycast"/> and recursive calls. /// </summary> /// <param name="frameMovement"></param> public void CollisionCheck(Vector3 frameMovement) { pointUp = Position.position + (CapsuleCollider.center + Vector3.up * (CapsuleCollider.height / 2 - CapsuleCollider.radius)); pointDown = Position.position + (CapsuleCollider.center + Vector3.down * (CapsuleCollider.height / 2 - CapsuleCollider.radius)); if (Physics.CapsuleCast(pointUp, pointDown, CapsuleCollider.radius, frameMovement.normalized, out capsuleRaycast, Mathf.Infinity, CollisionMask)) { float angle = (Vector3.Angle(capsuleRaycast.normal, frameMovement.normalized) - 90) * Mathf.Deg2Rad; float snapDistanceFromHit = skinWidth / Mathf.Sin(angle); Vector3 snapMovementVector = frameMovement.normalized * (capsuleRaycast.distance - snapDistanceFromHit); //float snapdistance = capsuleRaycast.distance + skinWidth / Vector3.Dot(frameMovement.normalized, capsuleRaycast.normal); //Vector3 snapMovementVector = frameMovement.normalized * snapdistance; snapMovementVector = Vector3.ClampMagnitude(snapMovementVector, frameMovement.magnitude); Position.position += snapMovementVector; frameMovement -= snapMovementVector; Vector3 frameMovementNormalForce = HelpClass.NormalizeForce(frameMovement, capsuleRaycast.normal); frameMovement += frameMovementNormalForce; if (frameMovementNormalForce.magnitude > 0.001f) { Vector3 velocityNormalForce = HelpClass.NormalizeForce(Velocity, capsuleRaycast.normal); Velocity += velocityNormalForce; ApplyFriction(velocityNormalForce.magnitude); } if (frameMovement.magnitude > 0.001f) { CollisionCheck(frameMovement); } return; } else { Position.position += frameMovement; } }