Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by blagopenev · Mar 13, 2011 at 12:07 PM · weaponfireselect

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

 }

}

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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!!! :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blagopenev · Mar 14, 2011 at 09:25 AM 0
Share

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 !

avatar image zmar0519 · Mar 14, 2011 at 07:53 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges