Hi peeps,
I’m Trying to create a gun pick up but struggling to get the script right. What I want is to have a trigger object in the scene (which I’ve created with ‘On trigger event’) and when my character goes over the gun object it picks it up. Ideally I want it to place the gun in the hand. I have a the arm as part of the main body of the object called RightArm. The body is called Jack. I’ve tried lots of things and would really appreciate some help. I’m assuming I have to target the arm (being part of the main of the body object) and then instansiate to that arm somehow… Here’s the script so far which seems to instantiate the main parent object onto the scene rather than the gun in the hand. I have clearly got this completely wrong :
using UnityEngine;
using System.Collections;
public class GunPickUp : MonoBehaviour
{
private GameObject _GameManager;
public GameObject Jack;
public GameObject Gun1;
//Finds the game managers
void Start()
{
_GameManager = GameObject.Find("_GameManager");
}
//Locates the Tag for self and ball to kill them
void OnTriggerEnter(Collider other)
{
if (other.tag == "Gun1")
{
_GameManager.GetComponent<GameManager>().GunPickUp();
PickUpGun();
}
}
// Instansiates Jacks body (Replaces Jack's body with Jack'sw Break body)
void PickUpGun()
{
{
// get a reference to the newly created object:
GameObject obj;
obj = (GameObject) Instantiate(Gun1, Jack.transform.position, Jack.transform.rotation);
// child the object to GunHolster:
obj.transform.parent = Jack.transform;
}
Many thanks for your time.
Rich