- Home /
Collision in between 2 objects moving away from each other?
I have a cube and a terrain in which the cube is moving away from the terrain at a regular pace . what i need is to enable gravity for any object that comes in between both of the objects(terrain & cube),much like an extensible collider .Is there any way to implement this using colliders or other stuff.thanks in advance.
Answer by sevensixtytwo · Sep 30, 2014 at 12:48 PM
You can use a CapsuleCastAll.
Essentially, it's a Capsulecast that stores all colliders within the Cast in an array. So you'll have to call it every update and specify where it starts and ends. Like so:
//Get distance of cube from floor
var distance : float = Vector3.Distance(cube.position,floor.position);
var hits : RaycastHit[];
hits = Physics.CapsuleCastAll (cube.position, cube.down * distance, 1.0, cube.down, distance);
for (var i = 0; i < hits.Length; i++) {
//Apply Gravity;
hits[i].rigidbody.AddForce(Vector3.down * Physics.gravity);
}
nice one sevensixtytwo thank you .script is perfect for my use. but can u tell me how to get the distance from terrain to cube which may be diagonal too.
You can probably Raycast downwards from the cube.
var terrain$$anonymous$$ask : Layer$$anonymous$$ask;
var distance : float;
var groundHit : RaycastHit;
if (Physics.Raycast(cube.position,cube.down,groundHit,1000.0f,terrain$$anonymous$$ask) {
distance = groundHit.distance;
}
Use this ins$$anonymous$$d of Vector3.Distance to get the distance from the cube and whatever is directly below it up to a distance of 1000.0f
You might have to setup a Layer $$anonymous$$ask to detect only your terrain and not any other objects below the cube.
Your answer
Follow this Question
Related Questions
Disabling objects from Collision detection 1 Answer
Objects rotating after falling on another object with Physics & Gravity? 1 Answer
Falling object on collision 0 Answers
Colliders not working with Character VS House 0 Answers
Collision details 1 Answer