- Home /
How to Pick up Gameobject
Alright this is the script i have so far and it is no where near complete...
using UnityEngine; using System.Collections;
public class CharacterUsage : MonoBehaviour {
public GameObject Arssenal01;
public GameObject Arssenal02;
public bool isHands = true;
public int Strength = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(isHands == true){
Hands();
}
else
{
Weapon();
}
}
public void Hands() {
}
public void Weapon() {
}
}
How could i make it where when you push the button 1 on your keypad it takes out hands and from there you could use some sort of raycast... i dont know but something so where i can pick up gameobjects... All help is appreciated...
Answer by Simple · Feb 22, 2011 at 07:44 AM
Make your hands and other weapons inactive meshes that flying in front of your camera. When you press button one of this objects activates(and became visible).
I used this code for this:(Maybe it is not what u need exactly, but hope it helps)
void Start() { SelectWeapon(1); }
void Update() { if (Input.GetKeyDown("1")) SelectWeapon(1); else if (Input.GetKeyDown("2")) SelectWeapon(2); else if (Input.GetKeyDown("3")) SelectWeapon(3); else if (Input.GetKeyDown("4")) SelectWeapon(4); else if (Input.GetKeyDown("5") || Input.GetKeyDown("l")) SelectWeapon(5); }
void SelectWeapon(int num) //need to check if weapon exists and has ammo {
for (int i=0; i<transform.childCount; i++) { //Activate weapon if (i == num) transform.GetChild(i).gameObject.SetActiveRecursively(true); // Deactivate all other weapons else transform.GetChild(i).gameObject.SetActiveRecursively(false); }
}
thanks man, you wrote the script perfectly clean but a couple of questions... Why is the fifth weapon also open with l? and do i add this to my code or remove everthing from the variables to the end of the coding of course leaving out the last bracket... Thanks for the help
Input.Get$$anonymous$$eyDown("l") is not necessary(fifth weapon also activates by pressing L). It is not exact code for you. Just a template how to make it. If it fits your code you can use it fully. But better get an idea and implement it accurately.
Your answer
Follow this Question
Related Questions
How to get info of object within certain range?(Javascript) 1 Answer
Getting GameObject from a grid (array) using raycasters 0 Answers
How to detect if a raycast ray stop hitting an object 1 Answer
I need help with raycasts and with color of gameobject [SOLVED] 3 Answers
Object picking from the scene view 1 Answer