So, I got this script from another forum post, and decided it wouldn’t be worth trying to bring up a dead thread so I decided to ask this again. The gravity gun script works, as long as the objects are in the air, not touching the terrain (doesn’t work if I create a box terrain either) if they are on the ground, it simply will not pick them up, if they are in the air, then it will. Help with this would be great. Here’s the code
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.GetButton("Fire1")) {
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)) {
if(hit.rigidbody) {
rigid = hit.rigidbody;
gravityGunState = GravityGunState.Catch;
// for debuging, remove it
print("force: " + currentForce);
}
}
}
}
else if(gravityGunState == GravityGunState.Catch) {
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(!Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Occupied;
}
else if(gravityGunState == GravityGunState.Occupied) {
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(Input.GetButton("Fire1"))
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.GetButton("Fire1"))
gravityGunState = GravityGunState.Release;
// for debuging, remove it
print("force: " + currentForce);
}
else if(gravityGunState == GravityGunState.Release) {
rigid.AddForce(transform.forward * currentForce);
currentForce = minForce;
gravityGunState = GravityGunState.Free;
// for debuging, remove it
print("");
}
}