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