I’m Wondering how to get a weapon when colliding with an object.
Here’s my code
function OnTriggerEnter(theCollider : Collider)
{
SelectWeapon(0);
}
function Start () {
// Select the first weapon
SelectWeapon(1);
}
function Update () {
// Did the user press fire?
if (Input.GetButtonUp ("Fire1"))
BroadcastMessage("Fire");
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
It’s from the fps tutorial on how to switch weapons and i just replaced GetButtonDown With OnTriggerEnter
and nothing happens.
You only answered part of my post. It takes two colliders to collide; you need both a collider on what you’ll walk into, and a collider on the player.
Add a Debug.Log() in your OnTriggerEnter() function and find out: is it getting called? If it’s not getting called, nothing’s going to happen. If it is, the bug is elsewhere.
function Start () {
// Select the first weapon
SelectWeapon(0);
}
function Update () {
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
function OnTriggerEnter (other : Collider) {
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
}
}
And i dont know were to put the "else if (Input) Stuff. Can someone please write this correctly for me?
You can’t put a function in the middle of another function.
function Start () {
// Select the first weapon
SelectWeapon(0)
}
function Update () {
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
}
}
function OnTriggerEnter (other : Collider) {
SelectWeapon(0);
}