Need help with throwing an object

ok so ive got an inventory system that when i pick up the rock i can move it into a holding slot and it apears in my hands what i want to do is throw it out of my hands and to do this i need to unparent it from my hands and turn is kinematic off here is what i have so far can anyone help me implement this?

var Throw : Transform;
 
function Update() {
    if(Input.GetButtonUp("Fire1")) {
        var projectile = Instantiate(Throw,
                                     transform.position,
                                     transform.rotation);
        projectile.rigidbody.AddForce(transform.right * 10000);
        Physics.IgnoreCollision(projectile.collider, collider);
    }
}


//what i need to do
//un parent from arms
//disable is kinemetic
//launch forward with set power and remove from hands so it dosent use clones

The weapons that are picked up are placed into this hierarchy of the hands
main camera
arms05
arm_armature
master
innerarm_r
arm_r
hand_r
melee

It is really hard to go from what you have here to what you say you need. That is the code above has nothing to do with getting an object from inventory and placing it in the hand. It is just some generic code, so at best we can only guess at how you are storing the object. Roughly: object.transform.parent = null; object.rigidbody.isKinematic = false; Physics.IgnoreCollision(object.collider, hand.collider); projectile.rigidbody.AddForce(transform.forward * 1000); Some of this is iffy. The direction to AddForce will depend on how you've set things up.

–

the inventory and adding it is already in place and does not need to be done all i am doing is adding a script to the rock being placed in it to be able to throw it and thanks for your reply i will try some of this tonight

–

ok ive got it working but sometimes when throwing the rock it throws it straight up into the air any ideas?

–

var Throw : Transform; function Update() { if(Input.GetButtonUp("Fire1")) { var projectile = Instantiate(Throw,transform.position, transform.rotation); parent = null; projectile.rigidbody.AddForce(transform.forward * 6000); Physics.IgnoreCollision(projectile.collider, collider); } }

–

1 Answer

1

Yeah I had the same problem with a fireball shooting off, its colliding with your player i’d imagine.

I’m just reimporting my assets after a unity crash so i’ll outline it if you need code lemmy know.

Basically I used an animation event to fire at the end of the throw animation(where the arm looked about right)

In this function I rotated the stone to the top level parents rotation(not the hand, but basically where your character is pointing) and then unparented it, moved it a couple of points forward then applied force, that way its well clear of your collider and anything else on your player.

Personally what i’d actually do if you’re doing a lot of this is create an empty gameobject thats parented to your player but sits a little way infront of him, then with anything your launching move it to be parented by the empty object, align it, unparent, apply force. A bit like a launch platform?

Hope that helps

Can provide code if needed

Marc

ok i understand but this rock sits in the players hands before he throws so could you help me in moving it up and out of the way of the hand before it is shot?

–

Can't you just use Physics.IgnoreCollision() between the object and the hand?

–

You could do yeah, I hadn't come across that before, i'll post some code in a lil while PvTGreg, just down the shop getting some smokes!

–

on i was also wandering if its possible to destroy the game object that remains in the hand? would it be as simple as placing Destroy (gameObject);

–

ok it seems it is but i need to find away to remove the rock from the inverntory as well

–