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