- Home /
Problem with my weapon change script
The problem is that the player can't fire. After i press (1) and try to fire nothing happened. I am trying to make a script that when i press button (1) to shoot with weapon 1 and pressing (2) to shoot with weapon 2.
var rotatespeed : float = 3.0f ;
var BulletPrefab : Transform ;
var BulletPrefab1 : Transform ;
function Update () {
// Mouse controll
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if(playerPlane.Raycast (ray, hitdist))
{
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotatespeed * Time.deltaTime);
}
// Bullet Spawn
if(Input.GetKeyDown("1"))
{
var weapon1 = 1 ;
Debug.Log("press 1") ;
if(Input.GetButtonDown("Fire1") && weapon1 == 1) {
var bullet = Instantiate(BulletPrefab, GameObject.Find("bulletspawn").transform.position, transform.rotation );
bullet.rigidbody.AddForce(transform.forward * 1000) ;
Debug.Log("Fire weapon 1") ;
}
}
if(Input.GetKeyDown("2"))
{
var weapon2 = 2 ;
Debug.Log("press 2") ;
if(Input.GetButtonDown("Fire1") && weapon2 == 2)
{
var bullet1 = Instantiate(BulletPrefab1, GameObject.Find("bulletspawn").transform.position, transform.rotation );
bullet.rigidbody.AddForce(transform.forward * 1000) ;
Debug.Log("Fire weapon 2") ;
}
}
}
Answer by zmar0519 · Mar 13, 2011 at 12:20 PM
As to your question, I suggest a slightly different logic for the weapons. What I did, was make a gun script that contained a var for the fire key. Then, in a different script(a gun manager), I made an array of gameObjects that were the guns. Then, in the update function, I used a for loop like this one:
function Update()
{
for(i = 0; i < [gun var here].length; i++)
{
if(Input.GetButtonDown([gun var here][i].GetComponent(Gun).[key to activate var here]))
{
ChangeToGun(i)
}
}
}
Then you would have a change to gun function like this one:
function ChangeToGun(gunInt : Int)
{
for(i = 0; i < [gun var here].length; i++)
{
[gun var here].active = false;
}
[gun var here][gunInt].active = true;
}
Then you would use the instantiate command you used above in a gun script. Just make sure that it has a key to activate var:
var keyToActivate : KeyCode;
Also, just a hint, look up Time.time in the documentation, it will give a simple machine gun script!!! :)
Thank for the quick answer! I have found a solution for my problem.
i just add : var weapon : boolean = false ;
In Update function: if(Input.Get$$anonymous$$eyDown("1")) { weapon = true ; weapon1 = false ; } if(Input.GetButtonDown("Fire1") && weapon == true) { ..... and the rest of the code.
Thanks again !
Thats great. Just make sure you mark an answer as your selected one so that you don't keep getting more responses to an answered question. As of now, your question still appears in the unanswered tab.
Your answer
Follow this Question
Related Questions
need help with [] 1 Answer
input fire error? 3 Answers
Shooting Pistol and Machine Gun 2 Answers
press a key, timing sequence? 0 Answers
how to select weapon in main menu 1 Answer