- Home /
Movement with collision detection but without a CharacterController
How can I move a scene object A, that contains a Collider component as well as a RigidBody, in order that if such object A is blocked by another static object B, that also contains a Collider component, object A's translation will be limited to object B's bounds? All of this WITHOUT using a CharacterController or RigidBody.AddForce.
To be move specific: I am only interested in limiting specific movements. Gravity, rotation, slope limits, inertia, all of these can be disconsidered. I originally was working just by updating the object A transform's position and manually checking the scene's bounds (sometimes with the help of OnTriggerEnter, sometimes with a custom solution), but the workload has become overwhelming.
Any suggestions? Am I going at it the wrong way? Thanks in advance.
So if A hits B, A can only move in object B's bounds?
If A receives a $$anonymous$$ove input of Vec3(5,0,0), and B is at an exact distance (diff) of Vec3(2,0,0), A should only be allowed to move Vec3(3,0,0). In the case of the same input again, A would not move at all. B in no case moves or should be affected by the collision.
Answer by Andrew_Kenady · Sep 03, 2014 at 01:24 PM
I'm a bit confused by your question. The way I've interpreted what you've asked is "how can I preserve collision while not using a character controller or rigidbody forces?".
If this is the case, all you need to do is update your object's transform.position at runtime. As long as both objects have colliders and rigidbodies, physics will still act upon them and prevent them from occupying the same space.
So, for instance, let's say object 1 is a sphere and object 2 is a wall (cube). You can give both objects a collider (sphere and box, respectively) and a rigidbody. You could choose to make your wall's rigidbody kinematic, so as to freeze it in world space. Then you might choose to freeze your sphere's rigidbody x, y, and z rotation (depending on the application).
Then, all you would need to do is update your sphere's transform.position from code, and the colliders should work as expected when the sphere reaches the wall.
Hope this helps!
Thanks Andrew. This was my original strategy but I recall the "physics will still act upon them and prevent them from occupying the same space" premise failing. Later when I get the opportunity to put my hands on the project again I will give it a double-check, and afterwards some feedback.