- Home /
object passing through walls and floors
hi i have made a script for my "gravity gun" and i'm having an with it.the probleme is that the object i'm picking up goes through the floor or wall when i "hold" it agains it. i did a bit of testing and brainstorming and i figured that what made it pass through he wall or floor is because of the hold distance. so my question is, is there a way to prevent the holddistance from overriding the colliders so it does not got through obects?
here is what i have done so far:
var catchRange = 30.0;
var holdDistance = 4.0;
var minForce = 1000;
var maxForce = 10000;
var forceChargePerSec = 3000;
var layerMask : LayerMask = -1;
enum GravityGunState { Free, Catch, Occupied, Charge, Release};
private var gravityGunState : GravityGunState = 0;
private var rigid : Rigidbody = null;
private var currentForce = minForce;
function FixedUpdate () {
if(gravityGunState == GravityGunState.Free) {
if(Input.GetKey("e")) {
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)) {
if(hit.rigidbody) {
rigid = hit.rigidbody;
gravityGunState = GravityGunState.Catch;
}
}
}
}
else if(gravityGunState == GravityGunState.Catch) {
rigid.MovePosition(transform.position + transform.forward * holdDistance);
rigid.rigidbody.useGravity = false;
if(!Input.GetKey("e"))
gravityGunState = GravityGunState.Occupied;
}
else if(gravityGunState == GravityGunState.Occupied) {
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(Input.GetKey("e"))
gravityGunState = GravityGunState.Charge;
}
else if(gravityGunState == GravityGunState.Charge) {
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(currentForce < maxForce) {
currentForce += forceChargePerSec * Time.deltaTime;
}
else {
currentForce = maxForce;
}
if(!Input.GetKey("e"))
gravityGunState = GravityGunState.Release;
}
else if(gravityGunState == GravityGunState.Release) {
rigid.AddForce(transform.forward * currentForce);
currentForce = minForce;
gravityGunState = GravityGunState.Free;
rigid.rigidbody.useGravity = true;
}
}
@script ExecuteInEditMode
Your code looks good. $$anonymous$$ake sure rigid is in a layer that collides with whatever layer you have the walls in. Simple I know but it happened to me.
Answer by superluigi · Sep 20, 2013 at 05:04 AM
You can try something like this to stop rigid from going through walls.
var hit : RaycastHit;
if (Physics.Linecast(transform.position, rigid.position, hit, layermask))
{
var tempDistance = Vector3.Distance(transform.position, hit.point);
rigid.position = transform.rotation * Vector3(0, 0, tempDistance) + transform.position;
}
Your answer
Follow this Question
Related Questions
Detecting if near the edge of an object ? 2 Answers
Checking Collision on instantiated object 0 Answers
My Rigid body is passing through my static mesh (non rigid) 1 Answer
In-game pickup not moving towards player after being collected 1 Answer
Does OnCollisionEnter only get called when the scripted object initiates the collision? 4 Answers