- Home /
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.
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;
}
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
Follow this Question
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