Parented Game object with collider not colliding
Hi, I have a 1st person player, using character controller. I have some walls. The Player colliders and wont go through the walls. GREAT SO FAR! The player can "pick up" and electric guitar. This is an game object with a box collider on it. Hovering over it picks it up, which means its position is placed right in front of the view, and is parented to the camera, so that it moves with you.
HOWEVER! When I move towards a wall, the guitar will go through the wall, the player will stop, but as you move around the guitar will intersect the wall.
How can I stop this from happening?
Answer by Static-Dynamo · Jun 02, 2016 at 11:07 PM
When something is parented it pulls all of it's locational data from the parent object. This will override things like collisions.
One way to fix your issue would be to add an additional collider to the parent object that is at the location of the guitar object. Then it will not let the guitar pass through the wall.
There are multiple ways to tackle this. The simplest way might be to not have the guitar become a child of the character controller at all, instead have it's transform position constantly updating to be at a position of an empty or invisible game object.
Another way to do it would be to create a collider that is the same shape, size, and orientation of the guitar's collider on the parent object when the guitar get's picked up.
@Static Dynamo Thanks for that, first sentence explains a lot!
Im drawn to your first suggestion to move the guitar by its transform position, rather than parenting.
The second suggestion I'm not sure about. when you say make an additional collider, in the position of the guitar WHEN it's parented. Firstly, if I parent that additional collider won't that have the same issue, as in, not work as its taking its info from its parent, or does that only happen when you parent during gameplay? I have tested this of course, by parenting the guitar from start, (this was another solution I was considering, as in enabling and disabling a hidden guitar on pickup, but I rejected this cos it still didn't collide.
$$anonymous$$ark
So you wouldn't make a new collider and parent it, you would make a new collider as a part of the parent. Not part of the child. You wouldn't parent that additional collider you would just attach it to the parent.
By attached, you mean what? Because it would have to be parented in some way would it not? I will still have to disable it at game start and enable it when needed, or else its going to collide the whole time, even when NO guitar is picked up.
... oh but wait.. do you mean I have the box collider (for surrounding the guitar) on the same component as the char controller...(which acts as the collider for the player).. Yes.. well unfortunately I can´t do that. The camera has a mouselook type code on it, and so in order for the guitar to appear to be "fixed" in the camera view it has to be parented to the camera parent. which makes it a child of the char controller. Thats not a problem with your first suggestion, as I can easily have the guitar follow a dummy GO that IS parented to the camera.
So I think Ill start with that solution... slightly concerned if thats a system drag at all... I dont know about that stuff. but Ill test and see if its robust and smooth.
Thanks for you help @Static Dynamo
Answer by markfrancombe · Jun 03, 2016 at 09:49 AM
@Static Dynamo said
Yes but the character controller doesnt ACTUALLY rotate itself so when the camera rortaes AROUND the char controller, the child object will stay where it was.You can actually create a collider that has its center is offset from the position of the object that you have the collider attached to.
This will mimic the behavior of a child object that is offset from the parent without needing to do any child/parent relationship. So it can still work even if you have a mouselook script moving the camera around, because the collider attached to the camera is offset from the camera's transform position.
And that collider WILL rotate around the char controller WITH the camera. So still not convinced by this, (or not getting it) Does this pic (whiteboard equivalent) help?
Maybe not?
:)
Mark
Aha! You are 100% correct. It was late and I did not read your last response as carefully as I should have. If your camera is a child of the character controller this won't work. You've got it.
Im still holding out for your very first suggestion... I hope that will work...