How can I improve my Push/Pull object script?

Hello everyone,

I have been trying to get a push/pull script to work and finally I got something to at least work. I am work on a 2D game, and I am trying to push/pull a cube on a ground. The following is my script so far:

var hit : RaycastHit; //Holds some of the properties the raycast hit.
var pushforce : float = 1; // Pushing Force
var pullforce : float = 1; // Pulling Force
var forceMode : ForceMode = ForceMode.VelocityChange; //Force type

function Update () {

//Get PlatformInputController script
var rotate : PlatformInputController = GetComponent(PlatformInputController); 

// Get CharacterMotor script
var jump : CharacterMotor = GetComponent(CharacterMotor); 

// Check for any objects at a distance of 1
if (Physics.Raycast(transform.position, transform.forward,hit,1)) {

// If tagged pushable and Action button is pressed
	if (hit.transform.gameObject.tag == "Pushable" && Input.GetButton("Action")){ 
	
	// Character cannot rotate 
	rotate.autoRotate = false; 
	
	//Character cannot jump
	jump.jumping.enabled = false; 
	
	// Get the distance between character and object position
	var D : Vector3 = transform.position - hit.transform.position; 
	
	// If on the left of object and move away from the object
	if (Input.GetAxis("Horizontal") < 0 && D.x < 0){
	// Apply pull only when the distance is greater than 0.5
	if (hit.distance>0.5){ 
	hit.rigidbody.AddForceAtPosition(D*pullforce, hit.transform.position, forceMode); 
	}
	}
	// If on the left of object and move towards the object
	if (Input.GetAxis("Horizontal") > 0 && D.x < 0){ 
	// Apply push only when the distance is less than 0.5
	if (hit.distance < 0.5){ 
	hit.rigidbody.AddForceAtPosition(-D*pushforce, hit.transform.position, forceMode);
	}
	}
	// If on the right of object and move towards the object
	if (Input.GetAxis("Horizontal") < 0 && D.x > 0){ 
	// Apply push only when the distance is less than 0.5
	if (hit.distance<0.5){ 
	hit.rigidbody.AddForceAtPosition(-D*pushforce, hit.transform.position, forceMode);
	}
	}
	// If on the right of object and move away from the object
	if (Input.GetAxis("Horizontal") > 0 && D.x > 0){ 
	// Apply pull only when the distance is greater than 0.5
	if (hit.distance>0.5){ 
	hit.rigidbody.AddForceAtPosition(D*pullforce, hit.transform.position, forceMode);
	}
	}
	}
    // Otherwise character can rotate and jump again
	else{ 
	rotate.autoRotate = true; 
	jump.jumping.enabled = true;
	}
}
}

This script works fine(I have commented it if there is still anything not clear please ask), but when in action it is not smooth at all. Two problems that I notice is that my force is dependent on the distance of my character to the cube hence sometimes I get a fast motion and sometimes I get a slow motion when I push or pull. The other problem I have is that when I have the rotation of the cube not fixed, if I try to push or pull the cube it will start moving by rotating forward or backward. At the end I need the rotation to be not fixed since I want to throw the cube of a ledge and it should rotate by gravity as it falls. So making the cube fixed rotation is not an option for me.

I am new to Unity and scripting and I hope if anyone can at least lead me to the right direction of thinking, or what methods I should use in order to have a more solid script.

Any help is appreciated.

Thank you very much.

For the rotate, AddForceAtPosition is designed to rotate the object as if you were pulling/shoving it from a certain point. To not rotate an object extra, just use AddForce. It’s like pushing someone on a swing, but making sure you do it dead-center, so they don’t twist or flip.

For the force, the standard trick is to use the normalized distance vector (which is often call direction.) Very common trick for moving, and lots of places to look up an explanation better than mine.

In the above, D is the entire line between puller and pulled. If you pull based on D, clearly something twice as far away will get pulled twice as hard. D.normalized is D reduced to a standard-length arrow pointing the way to go. Normalizing “takes away” the distance, leaving just direction.