Improved "Gravity Gun" script

I made some improvements to the "gravity gun" script today, and got rid of the janky movement and rotation issues.  The script now is set as a child to the player which smooths the movement out.  Velocity calculations are now based on the fixed time scale, with a variable for delaying the movement so that it acts smoother after being caught on environmental objects.  I think this is basically the final form of the "carry" function.  Now, logic for picking up and dropping objects needs to be added.

Once again, this is in Javascript

#pragma strict
// attach this script to your main camera

// this is the object the player is holding
var obihave : GameObject;
// this is how far in front of the player the object is
// carried
var carrydistance : float;
// this is how much delay is applied to the object as it
// attempts to move toward the "carry point."  Must not
// be lower than 1.
// higher values makes the object move more smoothly when
// "bouncing" back to your hand after getting caught on
// something in the environment.
var drag : float = 1;
// this is the distance above which the object will be dropped
var maxdist : float;

function Start () {


function FixedUpdate () {
if (obihave != null)

// this function does all the "carrying" work
function carryit()
// this is the point the object is trying to reach
var carrygoal : Vector3;
carrygoal = transform.position + (transform.forward * carrydistance);

// set rigidbody variables
var r : Rigidbody;
r = obihave.GetComponent(Rigidbody);
r.useGravity = false;
r.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;

// parenting the object to the player eliminates janky movement
obihave.transform.parent = transform;

// to eliminate some issues when looking directly down,
// the object layer can be changed here to a layer that won't
// collide with the player

// this sets the object's velocity based on both distance and the
// "resolve speed," in the direction of carrygoal
var force : Vector3;
force = carrygoal - obihave.transform.position;
force = Vector3.Normalize(force);
var dist : float;
dist = Vector3.Distance(carrygoal,obihave.transform.position);
force = (force * dist) / (Time.fixedDeltaTime * drag);
r.velocity = force;

// drop the object if it's too far from the goal
if (dist > maxdist)

// function for dropping the object.
function dropobject()
var r : Rigidbody;
r = obihave.GetComponent(Rigidbody);

obihave.transform.parent = null;
obihave = null;
r.useGravity = true;
r.constraints = RigidbodyConstraints.None;
r.velocity = Vector3(0,0,0);

1 comment:

  1. Hi everyone, I already posses quite a few songs in my playlist, but I need more. I've already listened to/possess full discographies of bands like Backstreet Boys, Boyzone, Michael Learns to Rock, R.E.M, Coldplay, The Corrs, Simon & Garfunkel, Secondhand Serenade, Linkin Park, Evanescence, Nickel Creek, Trading Yesterday, The Fray, Switchfoot, Snow Patrol, OneRepublic, Breaking Benjamin, Secret Garden, Enya, Iron & Wine, Celtic Woman, Josh Groban, Westlife, Lady Antebellum, Ryandan, Rascal Flatts, Death Cab for Cutie and so on, so you may skip songs from these bands since I most likely have heard them.

    INSPERATO by Kevin Mongelli