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?! =)
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.
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;
}
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!
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?
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;
}
}
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);
}
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.