Last active
August 29, 2015 14:01
-
-
Save DomDomHaas/d975c1625dd82dc49a26 to your computer and use it in GitHub Desktop.
Unity Weapon switch with Bone Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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