Hi!
I Have a UMP And A M21 In My Game
I’m Trying To Make A Simple Weapon PickUp Script For My FPS Game , For The Moment There Are Only 2 Weapons To Pick up And I Stopped For Testing ,All Works Fine , But When i Pick up The M21 That Uses The Second Pick Up Function Of The Code It Pick Up The UMP And Throws The M21 , I Tried Inverting The Weapons Codes , I Pickup M21 And Throw Down The UMP , It Can Work Only If I Keep Pressing E. So I Think i Should Call Them Both At The Same Time.
Here Is The Code :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PickUpWeapon : MonoBehaviour {
//Text To Ask The User If He Wants To Pickup The Weapon
public GUIText PickUpText;
//Weapon In Player Hands Variables
public GameObject M21Model;
public GameObject UMPModel;
public GameObject MP7Model;
//Array Of The Guns We Can Pickup
public GameObject[] M21PickUp;
public GameObject[] UMPPickUp;
public GameObject[] MP7PickUp;
//The Weapon We Throw When We Get Another One
public Rigidbody M21PickUpThrow;
public Rigidbody UMPPickUpThrow;
public Rigidbody MP7PickUpThrow;
//The Gun We Should Throw
private Rigidbody GunToThrow;
//Current Weapon We Are Holding
public int WeaponHoldIndex;
//Distance To PickUp The Weapon
public int distToPickUp;
// Use this for initialization
void Start () {
//Config The Current Weapon
if (M21Model.activeSelf)
WeaponHoldIndex = 0;
if (UMPModel.activeSelf)
WeaponHoldIndex = 1;
if (MP7Model.activeSelf)
WeaponHoldIndex = 2;
//Desactivate PickUp Text
PickUpText.gameObject.SetActive(false);
//Collect Pickup-able Weapons GameObjects
M21PickUp = GameObject.FindGameObjectsWithTag("M21PickUp");
UMPPickUp = GameObject.FindGameObjectsWithTag("UMPPickUp");
MP7PickUp = GameObject.FindGameObjectsWithTag("MP7PickUp");
}
// Update is called once per frame
void Update()
{
//Check All The UMP PickUpAble In The Array
for (int i = 0; i < UMPPickUp.Length; i++)
{
//Checks Distance Between Player And Weapon
if (Vector3.Distance(transform.position, UMPPickUp*.transform.position) <= distToPickUp)*
{
//Activating Pick Up Text
PickUpText.text = “Press E To Pickup HK-UMP”;
PickUpText.gameObject.SetActive(true);
if (Input.GetKeyUp(KeyCode.E))
{
//Which Weapon We Should Throw?
if (WeaponHoldIndex == 0)
GunToThrow = M21PickUpThrow;
if (WeaponHoldIndex == 1)
GunToThrow = UMPPickUpThrow;
if (WeaponHoldIndex == 2)
GunToThrow = MP7PickUpThrow;
//Weapon Throwing
Rigidbody thr = Instantiate(GunToThrow, new Vector3(UMPPickUp.transform.position.x, UMPPickUp_.transform.position.y + 3, UMPPickUp*.transform.position.z), Quaternion.identity) as Rigidbody;
//Initialize Arrays After Weapon Throwning*
M21PickUp = GameObject.FindGameObjectsWithTag(“M21PickUp”);
UMPPickUp = GameObject.FindGameObjectsWithTag(“UMPPickUp”);
MP7PickUp = GameObject.FindGameObjectsWithTag(“MP7PickUp”);
//Activating The Gun We Just PickedUp And Desactivating Others
UMPModel.SetActive(true);
MP7Model.SetActive(false);
M21Model.SetActive(false);
//Playing The PickUp Animations
UMPModel.animation.Play(“PickUp”);
//Set The Weapon Index
WeaponHoldIndex = 1;
//Desactivate pickup text
PickUpText.gameObject.SetActive(false);
//Get Rid Of The Weapon (Cannot Delete Item From An Array)
UMPPickUp*.transform.position = new Vector3(999999, 999999, 999999);*
}
}
else PickUpText.gameObject.SetActive(false);
}
//Same As Above
for (int i = 0; i < M21PickUp.Length; i++)
{
if (Vector3.Distance(transform.position, M21PickUp*.transform.position) <= distToPickUp)*
{
PickUpText.text = “Press E To Pickup M21 Barret”;
PickUpText.gameObject.SetActive(true);
if (Input.GetKeyUp(KeyCode.E))
{
if (WeaponHoldIndex == 0)
GunToThrow = M21PickUpThrow;
if (WeaponHoldIndex == 1)
GunToThrow = UMPPickUpThrow;
if (WeaponHoldIndex == 2)
GunToThrow = MP7PickUpThrow;
Rigidbody thr = Instantiate(GunToThrow, new Vector3(M21PickUp.transform.position.x, M21PickUp.transform.position.y + 3, M21PickUp*.transform.position.z), Quaternion.identity) as Rigidbody;
M21PickUp = GameObject.FindGameObjectsWithTag(“M21PickUp”);
UMPPickUp = GameObject.FindGameObjectsWithTag(“UMPPickUp”);
MP7PickUp = GameObject.FindGameObjectsWithTag(“MP7PickUp”);
UMPModel.SetActive(false);
MP7Model.SetActive(false);
M21Model.SetActive(true);
M21Model.animation.Play(“PickUp”);
WeaponHoldIndex = 0;
PickUpText.gameObject.SetActive(false);
M21PickUp = GameObject.FindGameObjectsWithTag(“M21PickUp”);
UMPPickUp = GameObject.FindGameObjectsWithTag(“UMPPickUp”);
MP7PickUp = GameObject.FindGameObjectsWithTag(“MP7PickUp”);_
_M21PickUp.transform.position = new Vector3(999999, 999999, 999999);
}
}
else PickUpText.gameObject.SetActive(false);
}*_
* }*
}