Switching Gun Script

http://www.youtube.com/watch?v=whI5uQi0bs0 That is what it looks like in game

function Awake()
{
// Select the first weapon
SelectWeapon(0);
}

function Update()
{
// Did the user press fire?
if (Input.GetButton (“Fire1”))
BroadcastMessage(“Fire”);
if (Input.GetKeyDown(“1”))
{
SelectWeapon(0);
}
else if (Input.GetKeyDown(“2”))
{
SelectWeapon(1);

}
}

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);
}
}

Can anyone edit this script to not have the guns zoomed in when switched?

I do not think this is a script issue. I don’t see a zoom in your script.

This is an edit of what you have to make using it a bit simpler. You are relying on child counts in your script and that is very hard to control. I suggest using an array that you create an array and use it instead of children.

var weapons : GameObject[];
private var currentWeapon : GameObject;

function Start()
{
	// Select the first weapon
	for(var i=0; i<weapons.Length; i++)
		currentWeapon.SetActiveRecursively(false);
	SelectWeapon(0);
}

function Update(){
	// Did the user press fire?
	if (Input.GetButton ("Fire1"))
		BroadcastMessage("Fire");
	if (Input.GetKeyDown("1"))
		SelectWeapon(0);
	if (Input.GetKeyDown("2"))
		SelectWeapon(1);
}

function SelectWeapon(index : int)
{
	if(currentWeapon)
		currentWeapon.SetActiveRecursively(false);
	if(index > weapons.Length) 
		return;
	currentWeapon=weapons[index];
	currentWeapon.SetActiveRecursively(true);
}

You then need to add the weapons onto the array. Pay attention to where the weapon is. If you have a zoom script then please post that, it may need to be added to this script.

It’s from the FPS tutorial, I think they just chose to do it that way so the Player prefab was neatly organized (the Player object had an empty “weapons” child object, and all the guns were children of that, and this script was on the weapons object).

using UnityEngine;
using System.Collections;

public class ScopeC : MonoBehaviour {

public bool Aim;

public GameObject CAM;

// Update is called once per frame
void Update () {

if(Input.GetMouseButtonDown(1) ) {

CAM.active = true;

}

if(Input.GetMouseButtonUp(1) ) {

CAM.active = false;
}

}
}

This is the zoom script but i only have it on gun 1

The assumption that I have is that all the guns are placed on the character and move with the hand’s. I also assume that this is not the only script that controls weapons. So zooming would be controlled via a different script. That script would need to know that you have swapped weapons and to stop zooming.

Perhaps you could send a message to the player saying “StopZooming” and the zoom script would know to quit it.

Hmm… this is not enough script. What tells the player to animate to the aim? This simply says CAM is active

This could easily be added to the weapon controller. :wink:

var weapons : GameObject[];
var Aim : boolean;
var CAM : GameObject;
private var currentWeapon : GameObject;

function Start()
{
	// Select the first weapon
	for(var i=0; i<weapons.Length; i++)
		currentWeapon.SetActiveRecursively(false);
	SelectWeapon(0);
}

function Update(){
	// Did the user press fire?
	if (Input.GetButton ("Fire1"))
		BroadcastMessage("Fire");
	if (Input.GetKeyDown("1"))
		SelectWeapon(0);
	if (Input.GetKeyDown("2"))
		SelectWeapon(1);
	if (Input.GetMouseButtonDown(1))
		CAM.active = true;
	if (Input.GetMouseButtonUp(1))
		CAM.active = false;
}

function SelectWeapon(index : int)
{
	if(currentWeapon)
		currentWeapon.SetActiveRecursively(false);
	if(index > weapons.Length) 
		return;
	currentWeapon=weapons[index];
	currentWeapon.SetActiveRecursively(true);
}

Oh, and if you could please use Code tags when posting code.

Oh my bad, didn’t notice. :stuck_out_tongue: K i will try what you said.

Keep coming with the scripts… lol, I think the next part is actually part of the animation controller. That would be the final step.

If your paying attention though, you are cycling through how the aiming works. (and weapon swapping)

http://www.youtube.com/watch?v=whI5uQi0bs0 This is what happens

Yes, we are trying to find that.

CAM is a camera used for the site?

In your game, run it, look at the state of CAM. Is it an acitive camera? Swap guns until you get the error. pause the whole thing and look at the camera again… is it active?

We need to find out if there is another script that makes CAM active.

If so… did you swap the Fire1 to the right mouse button?

Adjusted code just in case you did:

var weapons : GameObject[];
var Aim : boolean;
var CAM : GameObject;
private var currentWeapon : GameObject;

function Start()
{
	// Select the first weapon
	for(var i=0; i<weapons.Length; i++)
		currentWeapon.SetActiveRecursively(false);
	SelectWeapon(0);
}

function Update(){
	// Did the user press fire?
	if (Input.GetMouseButton(0))
		BroadcastMessage("Fire");
	if (Input.GetKeyDown("1"))
		SelectWeapon(0);
	if (Input.GetKeyDown("2"))
		SelectWeapon(1);
	if (Input.GetMouseButtonDown(1))
		CAM.active = true;
	if (Input.GetMouseButtonUp(1))
		CAM.active = false;
}

function SelectWeapon(index : int)
{
	if(currentWeapon)
		currentWeapon.SetActiveRecursively(false);
	if(index > weapons.Length) 
		return;
	currentWeapon=weapons[index];
	currentWeapon.SetActiveRecursively(true);
}

Yeah the script you posted doesn’t work. :confused: When i enter the game i have to right click to see anything. Otherwise the screen is grey.

I have the CAM attached to the M4, which allows me to see throught the scope when i right click.
But there is no animation it just switches between the 2 cameras.

When i R click it goes to CAM
when i let go of R click it goes to main cam

Is the cam part of a weapon? if it was, then when you did the currentWeapon.SetActiveRecursively(false); would turn it on or off. thus giving you that type of error.

So what do i do to the script to fix that?

Also btw im using the code from the FPS Tutorial instead of yours because yours didn’t work. :stuck_out_tongue:

so what should i add to it to stop the weapons from being zoomed in when switched?

Think… if a camera is attached to a weapon, and you disable the weapon, it disables the camera… in reverse, if you enable the weapon, it enables the camera… its not a script thing… take the camera and link it to something else.

Hmm, should i take the camera and attach it to the Main Camera?

No, it should be attached to the root node of the character (the same place that the character controller is.)

Edit… I say this, but that may not be entirely accurate… You need a way to look up and down. I would have to see the entire project to see what you should hook it to. or if you should just have a script on the camera that mimics the gun.

This is how i have my hierarchy:

First Person Controller:
Graphics

Main Camera:

M4A1:
Camera

M1911:
Camera

so put it in First Person Controller?