Hello everyone! It’s my first thread/question in unity forums so please bear with me.
My problem is :
I have a bike object which has a rigidbody, and then it has a two sphere colliders (the wheels) parented to it.
The wheels doesnt have rigidbodies thus they dont produce OnCollision functions.
I want to check if any of them (or both) are colliding with the ground (or anything). The output should be two booleans like isFrontGrounded, isBackGrounded.
I’ve tried lots of different approaches to it but none works like i want it to. I tried to tag the colliders but got always false. When checked the names of ContactPoint, both the "this’ and “other” it couldnt produce the true state in boolean version,
meaning: it changed properly for only one wheel, even when both were touching the ground.
My code is here :
function OnCollisionStay(hit : Collision){
for (var contact : ContactPoint in hit.contacts){
if(contact.thisCollider.name == "wheelColliderB"||contact.otherCollider.name == "wheelColliderB"){
isWheelBGround = true;
}
else{ isWheelBGround = false;}
if(contact.thisCollider.name == "wheelColliderF"||contact.otherCollider.name == "wheelColliderF"){
isWheelFGround = true;
}
else{isWheelFGround = false;}
}
}
I’m not sure about working with wheel colliders, but unless I’m badly mistaken, the normal workflow for responding to Unity collisions would be giving each child a script which reports back to the parent about its status. Then the parent’s script needs a way to listen to its children’s scripts. If you are building these vehicles in the inspector (as opposed to procedurally with code), you could just have a WheelScript component on each wheel, then two WheelScript variables on your parent, and assign (drag-n-drop) both wheels to a WheelScript variable. In the WheelScript, you could check for a valid collision, and set a public bool (isGrounded) to true OnCollisionStay and false otherwise. Then in your Vehicle / Parent script, you could check WheelFront.isGrounded to know its status.
Hope this gives you some ideas. This isn’t the only way to achieve what you want, but as far as I know it’s a standard approach.
I wrote that they are sphere colliders
(wheels are buggy and unreliable). Also i stated that the child object with sphere colliders doesn’t have a Rigidbody component attached. Without it any script consisting OnCollision function doesnt produce a collision. ( I dont know why, I assume that this is a bug in PhysX )
Also I purposedly avoid attaching the Rigidbody component because it produces gargantuan amount of spazz.
I’m sorry, I didn’t pay close enough attention. Maybe you should explain what it is you’re trying to accomplish? If you’re trying to make a vehicle whose wheels respond to forces and collide with the world as a bike does in the real world, my suggestions ought to get you started. Basically, to get OnCollisionSomething or OnTriggerSomething, the script with those functions must be a component of the object with the collider/trigger. Afaik, a parent can’t query its children’s physics responses directly, so you need scripts on the children that report to the parent. Also, for a collision to be reported, at least one of the two colliding objects must have a rigidbody attached, which isn’t a bug (though it can be annoying). I’m pretty sure what your code is checking is whether your wheels are touching the vehicle, not the ground. Unless that script is on the ground?
There may be other ways around these limitations that I’m not intimately familiar with. Maybe try searching for others who’ve encountered similar troubles trying to make a bike vehicle? Also there’s a vehicle tutorial that does a decent job of explaining vehicle concepts and workarounds, but it’s been so long I don’t recall whether it speaks about bikes.
Good luck,
The Ironic thing is : when the Bike parent ( which has a Rigidbody ) collides with anything, OnCollision ocurrs. It even produces the names of the colliders attached (eg the children ones) BUT only once per cycle. So even when the two wheels stays on the ground only the first one is True.
Thats a very bad limitation.