Character movement overwrites collider

I have a FPS character and a sphere. I’ve put walls around the character which follow to position of the character, so that the player is always in a small tube, surrounded by walls:

void Update () {
   Vector3 innerWalls = gameObject.transform.position;
   Vector3 player = GameObject.Find ("playerController").transform.position;
   Vector3 temp;
   temp.x = player.x;
   temp.z = player.z;
   temp.y = innerWalls.y;
   gameObject.transform.position = temp;
}

Next, I’ve disabled the mesh renderer so that the walls are invisible for the player.

The reason I’ve done this, is because I want to make it so that if the player would go near the sphere, the walls would hit the sphere and the player should be able to “move” to sphere (basically the walls should be pushing it forward).

This doesn’t work, unfortunately. After some flickering, the sphere passes through the walls. If I would make the sphere move, it’s unable to go through walls be itself, but when the player is moving the walls, apparently it is.

I’m using a rigidbody on the sphere, but I disabled gravity on it because it floats in the air. I’m not using a rigidbody on the walls, only a collider. I tried adding a rigidbody to the walls too, but that makes the walls tip and possibly fall over after hitting the sphere. If you choose to Freeze the rotation of the walls, the sphere again passes through the walls.

I haven’t applied force because this would make it possible to “kick” the sphere away when hitting it, while it should just be pushed away (sticking to the wall).

Anyone knows what’s causing this issue or knows how to fix this?

You are setting the character position directly that isn’t recommended, you should use Vector3.MoveTowards or something that smoothes value over time to move your character position frame by frame.

Thanks.

Thanks for your reply. Please note that the script above is not the way I move my character. I move my character via CharacterController.Move. The code above is just the script I use for the walls to follow the character.

I am a little confused with your description, why you made the walls around the player, why you don’t let the character body hits the sphere?

Because I want there to be an invisible gap between the player and the sphere when the sphere is hit. A bit weird maybe but it’s a feature in my game :slight_smile:

No problem, so as your description states the wall goes through the sphere, right?

Does the sphere has Rigidbody component?

Does the walls has Rigidbody component?

If you want to push, move or apply any motion to an object physically, you need to enable physics on it.

What is type of Rigidbody on the Sphere?

What do you mean exactly by “type of Rigidbody”? It’s a Rigidbody-component with all the settings set to default, except “use gravity” is not checked.
So it’s:

Mass: 1
Drag: 0
Angular drag: 0.05
Use Gravity: no
Is Kinematic: no
Interpolate: none
Collision Detection: Discrete
Constraints: none

Could you provide some screenshots or videos?

Do you have tried using Rigidbody.MovePosition?

Yes I have. With the rigidbody attached to the walls, and using Rigidbody.MovePosition, the sphere doesn’t go through the walls, but my walls start to tilt when they hit the sphere, because they are affected by physics. When I freeze their rotation, the sphere passes through the walls again.

Try to remove your script from walls and throw walls on the sphere and check if they go through sphere or not if they don’t go through the sphere, the problem is the script, otherwise, we have some misconfigured physics settings.

If put a wall on top of the sphere (and put a rigidbody on the wall), the wall doesn’t go through the sphere. It falls on the sphere, and then falls off the sphere on the ground, so it behaves as it should: the collision works.

It seems we have a problem in our script but Did you try it?
Also, do you have tried to put the content of Update method of wall script to FixedUpdate?
Why didn’t you make the walls the child of the player game object?

Didn’t think about making it a child of the player game object. No movement script is needed if I make it a child of course… should have gone with this approach to start with.

Having it as a child of the player, the problems still exists, though. The sphere seems to hesitate if the wall hits it while I’m walking rather slow, but at a constant speed, the sphere seems to lose the fight and travels through the walls. My player itself (basically the camera since it’s first person) can’t move through the sphere, while the walls can…

FixedUpdate didn’t resolve this issue unfortunately.

Try to set the Collision Detection to Continous on both walls and sphere.

This works! Thanks. One thing I should note though: while testing this, I’ve made the walls visible so my player is inside the walls. When I run into the sphere, the sphere shows up inside the walls for a bit, and when I stop moving its outside again. So it is pushed forward by the walls (as it should), but it’s not perfect: half of the sphere is visible during the pushing while looking at it from inside the walls.

Not a big problem for me because the walls are invisible anyway, but thought I should mention…

EDIT: another problem I’m having now is that the player inside the walls moves forward while the wall is pushing. This should happen. The player should always be in the center of the walls. It stops working when I make the mass of the sphere a lot bigger than the mass of the wall: in this case it travels through the walls again. Also, the sphere gets force applied to, which it keeps even if the walls aren’t pushing anymore: the sphere keeps moving…

Here are some guides that might help you to solve this issue:

Hope this helps.
Thanks.

Maybe I should go for another approach. Let me clarify why I needed these walls:

the sphere in fact moves randomly. Basically I tell it to travel to a certain point and update that “nextPoint” every second (meaning it probably didn’t reach it’s previous point yet in that one second but that’s OK). This makes it move randomly around the map. The y-position doesn’t change, it’s always at a hight twice as high as my player.

Of course, it’s possible the ball travels over the players head. I don’t want this. The sphere should always respect a certain distance from the player. While this sounds fairly easy to do, I can’t figure it out because my character is able to move: you could steer the sphere away from the character, but if the user/character (which moves faster) decides to run to the sphere, it can travel beneath, which should be possible.

Maybe the wall-thing isn’t the proper way to do such thing? In any case, thanks for your help so far :)!

As you said you are updating the x position of the sphere that overrides the physics calculation, so it is better to try to remove this script and see if it works or not.