Basically I have a rigidbody instead of a character controller so I can’t check for collision flags easily, but am trying to basically fake it on collision enter and checking against the array, but am not having much luck. Below is a snippet of the code.
foreach ( ContactPoint contact in collision.contacts ) {
if ( contact.normal.x > 0 ) {
// right
} else {
// left
}
if ( contact.normal.y > 0 ) {
// above
} else {
// below
}
if ( contact.normal.z > 0 ) {
// front
} else {
//behind
}
}
The problem with the above is if my character hits its toe for example on a tiny bump it’ll register it as a left or right collision. So I want it to be probably from the middle of the gameobject to ensure it’s a wall or something. Any suggestions for doing the comparison properly?
Edit: Thought I’d mention that I already have a working implementation that casts a raycast up the side of the object and when it hits something I fire a custom collision event. This works wonderfully, but am concerned about performance as it’s non-stop type deal in order to detect realtime collisions. I figure the above would be better on performance, but if not then will continue with current usage.
The absolute worst thing you can do as a programmer is make assumptions about performance and then waste time changing code to fix these so called assumptions that you haven’t event tested.
Either profile the code and determine if it’s too slow before making any more changes, or finish the whole game first then circle back to this later if you have performance issues. Chances are by the time you finish programming a solid platformer you’ll know the best solution to this and won’t need to ask at all. =)
I’m a programmer by profession; I know this, but Unity Free does not come with a profiler. Regardless though I’d still have to implement both and test both to see the performance of both. Thus I’ve asked here for help on the usage as am new to Unity API and 3D programming (working with vectors), which I understood was the purpose of this forum category.
I asked here for help with my question… not criticism of said question… thanks.
Have you considered simply comparing the world coordinates of the two objects colliding using colision.transform.position.x/y/z? This works well as long as one of the object is either fairly smaller than the other, or is a primitive. Alternatively you can use ContactPoint.point which is a Vector3 to check the object’s world position (transform.position.x/y/z) against the point of contact. (EG If ContactPoint’s X value is higher than the object of contact then it means it was collided from the right.)
Yeah, I tried comparing point as well as normal (example above is using normals) but both have the same issue as follows.
-|
The - is the bump and the | is the cube. When the cube hits the bump (it’ll move over it just fine) it is registered as a left collision, which I don’t want. Strangle I’ve no idea why this works fine with a raycast along the side of the cube as the ray probably hits the bump too. Collision should basically be from mid-object and up so bumps or small steps are ignored (as those are navigated up fine).
I’m thinking I need to use InverseTransformPoint and inverse the point for accurate reading of where the collision occurred, but I need it to only do this if the collision point on the cube was from the middle of the cube or higher if that makes any sense, lol.
Kind of the point of the CharacterController is that it uses a Capsule Collider.
Where this is nice, I already made several Rigidbody colliders based off the same concept that the CharacterController comes with. (mostly because I didnt like certain situations the CharacterController got into.)
In doing this, I locked all rotation on the collider, so that it would pass just like a CharacterController. I then did a Spherecast downward using the radius of the capsule. This started about 0.1 above the bottom sphere (of the capsule) and extended 0.1 past that sphere. This gave a little give in the detection.
This now gives me a bit of space to detect things in. The OnCollisionEnter gives me good results when hitting objects anywhere. If I hit something that I wanted to hit, then I could do some things. If I was on the ground, the OnCollisionEnter would not hit, and if I hit a bump on terrain, I would also not get an OnCollisionEnter.
On the flip side, I would get OnCollisionStay, which, oddly enough, did me very little good. This is where the Spherecast came in handy. This told me if I was grounded. And gave me a normal to check against. Are you on the ground? or are you actually on the side of a hill? These are the things that the spherecast told me.
For your situation, yours is different. I believe that you need to do several Raycasts to figure out where about you are hitting something at. Low med and High should do it. All from object vertical center in the collision direction. (so the toe would shoot horizontally out low, mid, horizontally out mid and high same. This acts like a feeler.
Yeah, I’m finding raycasts being the best method for this. Trying to do it through the colliders just doesn’t seam to be working at all. Seams easier to just do a raycast left/right and if it hits something then it collided. I was just concerned with the possible performance problems of this, but as raycasts seam pretty standard usage I guess it’s not a problem.
Anyway, will just continue with current raycast implementation then. Thanks everyone!