- Home /
How to Equip and Dequip to hand in 2D
I'm making a top down shooter. I have a player. that player has a child in the position of their hand. this is where they will hold the object. how can I make it so if they press "e" and are in a range of the object, it is suddenly in the hand and only plays its script while it is in the hand? then if "e" is pressed again, how would I make the player drop it?
Answer by Ambrose998800 · Sep 26, 2015 at 05:52 PM
Try something like that:
if(Item.GetComponent(ItemScript).ItemPlace == "HandRight"){
if (HandRightUsed == false){
ItemInHandRight = Item.gameObject;
Item.gameObject.transform.position = HandRightEmpty.transform.position;
Item.gameObject.transform.parent = HandRightEmpty.transform;
HandRightUsed = true;
HandRightEmpty.SendMessage("ItemIsEquipped", true, SendMessageOptions.DontRequireReceiver);
}
}
Then add the function ItemIsEquipped(); to the ItemScript that activates further functions.
For dropping: unparent the Item again, set by its rigidbody 'isKinematic = false' and give 'ItemRigidbody.AddForce(float);' to it in desired direction.
Answer by Xeong-Hu · Sep 20, 2015 at 11:54 AM
Hmm Well. I'm gonna guess this is a sidescroller 2d game and not a first person shooter. Haha I was totally confused at first. But it's fairly simple. When ever you press the E button just make the Weapon's Object's Position Equal to Hands Position.
So like. Script mode.
public GameObject Weapon;
public GameObject Hand;
public bool NearWeapon;
public bool Holding;
if(KeyCode.E && NearWeapon == true){
Holding = true;
}
if(KeyCode.E && Holding == true){
Holding = false;
}
if (Holding == true)
{
Weapon.transform.position = Hand.transform.position;
}
if (Holding == false)
{
Weapon.transform.position = null;
}
Answer by mcarthur03 · Sep 25, 2015 at 09:27 PM
Thanks a lot! (its a top down shooter, 2D) there are two more things, when I de-equip the item I want it to throw. which I could probably do with some adding of force. but I also want the item to NOT work when it is de-equipped. it there a way I could disable its code when it isn't equipped?
the current script also comes up with these errors, I guess you gave me code that works in unity four and I just have to alter it to work in unity 5, but I cant seem to figure out how. :(
Assets/Scripts/Hold.cs(35,42): error CS0037: Cannot convert null to UnityEngine.Vector3' because it is a value type Assets/Scripts/Hold.cs(24,29): error CS0019: Operator
&&' cannot be applied to operands of type UnityEngine.KeyCode' and
bool'
Your answer
Follow this Question
Related Questions
How do I "equip" this weapon properly to a model? (IMG) 1 Answer
hold key unequip sword 0 Answers
Armour and armour changing 0 Answers
Picking up a flashlight 0 Answers
How to deal with different Weapon animations on Equip? 1 Answer