- Home /
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.
Answer by Owen-Reynolds · May 24, 2013 at 07:27 PM
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.
I have done the adjustments you suggested and the results are the same. The code is the following now:
var hit : RaycastHit; //Holds some of the properties the raycast hit.
var pushforce : float = 1; // Pushing Force
var pullforce : float = 1; // Pulling Force
var force$$anonymous$$ode : Force$$anonymous$$ode = Force$$anonymous$$ode.VelocityChange; //Force type
function Update () {
//Get PlatformInputController script
var rotate : PlatformInputController = GetComponent(PlatformInputController);
// Get Character$$anonymous$$otor script
var jump : Character$$anonymous$$otor = GetComponent(Character$$anonymous$$otor);
// 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.AddForce(D.normalized*pullforce, force$$anonymous$$ode);
}
}
// 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.AddForce(-D.normalized*pushforce, force$$anonymous$$ode);
}
}
// 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.AddForce(-D.normalized*pushforce, force$$anonymous$$ode);
}
}
// 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.AddForce(D.normalized*pullforce, force$$anonymous$$ode);
}
}
}
// Otherwise character can rotate and jump again
else{
rotate.autoRotate = true;
jump.jumping.enabled = true;
}
}
}
Any other ideas how I can avoid the cube to rotate? And any Idea how I can smoothen the pull to me or push forward?
Thanks for your reply.
Normalizing should be a huge visible difference. It may still not work correctly, but if a typical cube is 10 away, suddenly pulls should be 1/10th as much (which would be adjusted with pull force.)
Cubes will naturally tend to stick and roll over, no matter how you push then, just because of friction. It usually looks pretty cool (I pulled too hard -- it's tipping!) $$anonymous$$aybe test with friction=0. If that was the case, can play with angularDrag, swapping physics mats... . These are things you can test in a demo scene just by watching cubes slide down ramps.
Can also mess with using AddForceAtPos to push them near the bottom, but a giant pain and they can tip the other way.
Alright, I guess ill just have to play around until i've got it right. Thank you for your help.
After playing with my script I noticed that I had to normalize my D before adding the force. Push/Pull are now much smoother but still contain some problems that I hope to be fixing soon. Thank you for your help.
Your answer
Follow this Question
Related Questions
pulling objects across a collider 0 Answers
sucking in particles 3 Answers
pulling in objects one end, and releasing out the other 0 Answers
Add Force based on Raycast 1 Answer