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 matthew_tavares1 · Oct 06, 2011 at 09:23 PM · c#playerweaponswitchattacking

Weapon switching problem

I've been able to program two weapons switching between themselves, before I had only their attacks programmed, how much distance they can hit a target from, how much health they take away from the enemy with each hit, but when I added to be able to switch the objects(place the weapons within the game) I'm only able to access one attack when I play. So even though I can visually change between a sword and spear I'm not able to switch their attack with them. Can someone take a look at my script and see what's wrong?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAttacks : MonoBehaviour
 {
     public float attackTimerSword;
     public float coolDownSword;
 
     public float attackTimerSpear;
     public float coolDownSpear;
 
     public int equipedweapon;
     public int maxweapons;
 
     public GameObject Sword;
     public GameObject Spear;
     public Transform weapons;
     private GameObject currentweapon;
     private GameObject mySpear;
     private GameObject mySword;
 
     // Use this for initialization
     public void Start()
     {
         maxweapons = 2;
         equipedweapon = 1;
 
         attackTimerSword = 0;
         coolDownSword = 1.0f;
 
         attackTimerSpear = 0;
         coolDownSpear = 1.5f;
 
         mySword = Instantiate(Sword, weapons.position, weapons.rotation) as GameObject;
         mySpear = Instantiate(Spear, weapons.position, weapons.rotation) as GameObject;
 
         mySword.transform.parent = weapons;
         mySword.transform.position = weapons.position;
         mySpear.transform.parent = weapons;
         mySpear.transform.position = weapons.position;
         mySpear.SetActiveRecursively(false);
 
     }
 
     public void removeCurrentWeapon()
     {
         Destroy(currentweapon.transform.gameObject);
     }
     public void Update()
     {
         if (attackTimerSword > 0)
             attackTimerSword -= Time.deltaTime;
 
         if (attackTimerSword < 0)
             attackTimerSword = 0;
 
         if (attackTimerSpear > 0)
             attackTimerSpear -= Time.deltaTime;
 
         if (attackTimerSpear < 0)
             attackTimerSpear = 0;    
 
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             mySpear.SetActiveRecursively(false);
             mySword.SetActiveRecursively(true);
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             mySword.SetActiveRecursively(false);
             mySpear.SetActiveRecursively(true);
         }
 
         if (equipedweapon == 1)
         {
             if (Input.GetMouseButtonDown(0)) // Bõtão esquerdo. Só vai realizar essa ação no primeiro fram que for verificado o pressionamento do botão, os outros são igniorados (ButtonDOwn).
             {
                 if (attackTimerSword == 0)
                 {
                     attackTimerSword = coolDownSword;
                     foreach (var hit in Physics.RaycastAll(transform.position, transform.forward, 4f))
                     {
                         if (hit.collider.CompareTag("Enemy"))
                         {
                             EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
                             if (enemyHealth)
                                 enemyHealth.AdjustCurrentHealth(-10);
                             enemyHealth.healthTimer += enemyHealth.healthCooldown;
                         }
                     }
                 }
             }
         }
 
         if (equipedweapon == 2)
         {
             if (Input.GetMouseButtonDown(0)) // Bõtão esquerdo. Só vai realizar essa ação no primeiro fram que for verificado o pressionamento do botão, os outros são igniorados (ButtonDOwn).
             {
                 if (attackTimerSpear == 0)
                 {
                     attackTimerSpear = coolDownSpear;
                     foreach (var hit in Physics.RaycastAll(transform.position, transform.forward, 10f))
                     {
                         if (hit.collider.CompareTag("Enemy"))
                         {
                             EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
                             if (enemyHealth)
                                 enemyHealth.AdjustCurrentHealth(-20);
                             enemyHealth.healthTimer += enemyHealth.healthCooldown;
                         }
                     }
                 }
             }
 
         }
 
     }
 }

Equipedweapon 1 is the sword and equipedweapon 2 is the spear. When I renamed the attack to either MySpear or MySword I got both attacks mixed so on one click it would 10 damage on the other 20, so that didn't really work.

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
0
Best Answer

Answer by SilverTabby · Oct 06, 2011 at 09:27 PM

This seems like a simple problem:

You use the number equipedweapon to determine what attack to use, but a quick ctrl+f search shows that you never actually change the value of equipedweapon when you swap weapons.

quick fix:

 if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             mySpear.SetActiveRecursively(false);
             mySword.SetActiveRecursively(true);
             equipedweapon = 1;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             mySword.SetActiveRecursively(false);
             mySpear.SetActiveRecursively(true);
             equipedweapon = 2;
         }
Comment
Add comment · Show 1 · 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 matthew_tavares1 · Oct 06, 2011 at 11:34 PM 0
Share

That's strange I had tired that before but it didn't work, and now it's working fine, I probably misspelled something before. Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I switch weapon by pressing the same button? 2 Answers

Switching Weapons (C#) 2 Answers

Weapon Switching 1 Answer

Changing enum values 1 Answer

Multiple Cars not working 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