2.5d platformer - how to pull and object (crate)

I thought this one would be easy (heck, maybe it is), but I’ve been having a hard time figuring out a game plan for being able to have my character pull an object (crate). I’m new to scripting and have been working in javascript for about 3 weeks now. Im getting the hang of things, but the process is slow. Anyway, what I want to happen is for my character to walk up to a crate, and then be able to pull the crate by pressing and holding a button.

A nudge in the right direction would be wonderful. I’ve tried modifying the dragRigidbody script, but that seems to be a whole different thing all together.

I also just wanted to say hi to everyone. I’m a 3D artist who stumbled upon Unity about a month ago and have been completely obsessed since. How fun is this thing?! =)

Thanks,
Eric

www.ericprovan.com

this is so EASY! Just get a cube (or any other object) and then click Component > Physics > Rigidbody. Waller!

More Things

Mail me at Jamesterjim@hotmail.co.uk (or msn me) if you have any more questions / help

Ive studied and use rigidbodys quite a bit. I know that its very easy to push an object that is a rigidbody but Im wanting to actually pull the object with my character. I’ll check out the links you sent me, maybe I’m missing something =)

Edit: After some more research, it seems that parenting and unparenting may be my best bet. I’ll put a script up once I have one. I’m almost certain it wont work, but at least I’ll have a start.
Character comes in contact with gameobject.
Player presses a button and holds it.
The gameobject parents itself to the character.
Character moves (pulls) gameobject.
Player lets go of button.
Gameobject unparents from character.

Seems easy enough? =P

Thanks!
-Eric

So, here’s where I landed tonight. This does nothing. At least, it doesn’t seem to. But, hopefully I’m on the right path.
Again, I’m using a rigidbody character and I’m wanting to pull and object when I’m close to it and pressing a button.

var parentCrate : Transform;

function Awake() {
   parentCrate = transform.parent;
}

function Update() {
   if(Input.GetButton("Fire1"))
      transform.parent = parentCrate;

	if (Input.GetButton("Fire2"))
		transform.parent = null; 
}

Thanks,
Eric

www.ericprovan.com

Another night of trying, but no luck… I’m getting closer! =)
The script is now parenting the object correctly under the player in the hierarchy, but when I move the player, the object doesn’t follow.

Im attaching the script to my rigidbody player, and plugging in the object as target. Any help would be awesome!

var target : Transform;

function Update() {

   if(Input.GetKey ("left ctrl")) {
   
	target.transform.parent = transform;
	}
	
else {

	target.transform.parent = null; 

}
}

Thanks,
Eric

www.ericprovan.com

Have you still got the rigidbody on the crate? You need to remove that if the crate is to move along with the parent object.

Baby steps =) Thanks Andeee. Got things working finally, but one problem left. The crate parents itself to the player correctly now, it also moves with the player, but when I move the player, he moves at a much faster pace then the crate. Any clue how to fix this?

var target : Transform;



function Update() {
   if(Input.GetKey ("left ctrl")){
   
	transform.parent = target;
	rigidbody.isKinematic = true;
   rigidbody.useGravity = false; 
   
	}


   else {
   
   transform.parent = null;
   rigidbody.isKinematic = false;
   rigidbody.useGravity = true; 
	
	
	
} 
}

Is your “fire1” button triggering run or something else normally ?

Hmm, my character is set to always run. No input is used. And no, the fire1 button triggers nothing else.

Thanks though,
Eric

Ok, we’re on day 10 and still no luck lol. I will say this though, this has been a wonderful learning experience for me as I have zero scripting experience. Anyway, I decided to try doing this with joints. Heres the script ive put together. Its attached to a crate. The player collides with crate, a joint is made on the crate, player is connectde to that joint. Its doing everything wonderfully, but when I let go of “fire1”, it doesnt reset everything back to normal.

var currentPlayer : GameObject;
var myJoint : Joint;

function Start() {
	currentPlayer = GameObject.FindWithTag("Player");
   }

function OnCollisionEnter(collision : Collision) {

 if(collision.gameObject.tag == "Player")
  gameObject.AddComponent ("FixedJoint");
  myJoint = GetComponent(FixedJoint) ; 
   }
   
   function OnCollisionStay(collision : Collision) {
   
if (Input.GetButton ("Fire1")) {
  myJoint.connectedBody = currentPlayer.rigidbody; 
  rigidbody.isKinematic = false;
  rigidbody.useGravity = false; 
  }
  
  
}

Have you tried GetButtonDown or getkeydown? Also, can you make the joint inactive on keyup?

Bryan

Tried GetButtonDown (no luck), but havn’t tried GetKeyDown/Up. I can’t test this yet, but I will tonight. Thanks for your help. I’ll let you know how it goes.

var currentPlayer : GameObject;
var myJoint : Joint;

function Start() {
   currentPlayer = GameObject.FindWithTag("Player");
   }

function OnCollisionEnter(collision : Collision) {

 if(collision.gameObject.tag == "Player")
  gameObject.AddComponent ("FixedJoint");
  myJoint = GetComponent(FixedJoint) ;
   }
   
   function OnCollisionStay(collision : Collision) {
   
if (Input.GetKeyDown ("left ctrl")) {
  myJoint.connectedBody = currentPlayer.rigidbody;
  rigidbody.isKinematic = false;
  rigidbody.useGravity = false;
  }
 
if (Input.GetKeyUp ("left ctrl")) {
  Destroy (myJoint);
  rigidbody.isKinematic = false;
  rigidbody.useGravity = true;
  }
 
}

Woohoo, Got it working! I have no doubt that the script is sloppy, but its my first working script from scratch. Im quite proud. hehe =)
Thanks for the help.

private var currentPlayer : GameObject;
private var myJoint : Joint;

function Start() {
   currentPlayer = GameObject.FindWithTag("Player");
   }

function OnCollisionEnter(collision : Collision) {

 if(collision.gameObject.tag == "Player")
  gameObject.AddComponent ("FixedJoint");
  myJoint = GetComponent(FixedJoint) ;
   }
   
   function OnCollisionStay () {
   var currentPlayer : GameObject;
   currentPlayer = GameObject.FindWithTag("Player");
   
   
if (Input.GetButtonDown ("Fire1")) {
  myJoint.connectedBody = currentPlayer.rigidbody;
  rigidbody.isKinematic = false;
  rigidbody.useGravity = false;
  }
  
if(Input.GetButtonUp ("Fire1")) {
   Destroy (myJoint);
  rigidbody.isKinematic = false;
  rigidbody.useGravity = true;
 }
}

function OnCollisionExit(collision : Collision) {
Destroy (myJoint);
}
1 Like

Cool! One thing to consider is if the joint will be used again try .enabled= false. That should be more efficient compared to creating and destroying it.

Bryan