Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DomDomHaas/d975c1625dd82dc49a26 to your computer and use it in GitHub Desktop.
Save DomDomHaas/d975c1625dd82dc49a26 to your computer and use it in GitHub Desktop.
Unity Weapon switch with Bone Connection
// copy & past from see: https://www.youtube.com/watch?v=VH0ZfEv_ouc
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//attach 2 weapon prefabs to weapon1 and weapon2
//attach a bone Transform (head, foot, hand etc..) from your
//character to bone;
public Transform weapon1;
public Transform weapon2;
public Transform bone;
private Transform currentweapon;
void Start ()
{
//we attach weapon1 first;
currentweapon = Instantiate (weapon1, bone.position, bone.rotation) as Transform;
currentweapon.parent = bone;
}
public void removeCurrentWeapon ()
{
currentweapon.parent = null;
Destroy (currentweapon.transform.gameObject);
}
void Update ()
{
//press the right control button will swap between weapon1 and weapon2
// this is just for testing purposes, you will obviously have to do something much more
// sophisticated
if (currentweapon != null) {
if (currentweapon.name == "weapon1(Clone)") {
removeCurrentWeapon ();
currentweapon = Instantiate (weapon2, bone.position, bone.rotation) as Transform;
currentweapon.parent = bone;
} else {
removeCurrentWeapon ();
currentweapon = Instantiate (weapon1, bone.position, bone.rotation) as Transform;
currentweapon.parent = bone;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment