- Home /
How do I switch weapon by pressing the same button?
So far in my game, I have 3 attack forms: hand-to-hand combat, the summoning of a staff and the summoning of a scythe. What I'm working on currently is giving the player the ability to cycle through their attacks whenever they press the "S" key, but with the way I have it coded it doesn't really work.
To make this simpler, I've put the chunk of code needed to be edited on it's own script:
private Animator anim;
void Update()
{
//Summon staff
if (Input.GetKey(KeyCode.S))
{
anim.SetBool("summonStaff", true);
}
else
{
anim.SetBool("summonStaff", false);
}
//Scythe transition, which doesn't work of course because both animations basically play at the same time.
if (Input.GetKey(KeyCode.S))
{
anim.SetBool("switchToScythe", true);
}
else
{
}
}
}
Though I didn't think it was necessary, in case it is I'll put the rest of the code that has the player movements, the defense and attacks:
{ public float maxSpeed = 10f; public float jumpHeight = 45f; public float gravityScale = 11f; private Animator anim;
float moveDirection = 0;
bool isGrounded = false;
Vector3 cameraPos;
Rigidbody2D rb;
Collider2D mainCollider;
// Check every collider except Player and Ignore Raycast
LayerMask layerMask = ~(1 << 2 | 1 << 8);
Transform t;
// Use this for initialization
void Start()
{
t = transform;
rb = GetComponent<Rigidbody2D>();
mainCollider = GetComponent<Collider2D>();
rb.freezeRotation = true;
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
rb.gravityScale = gravityScale;
gameObject.layer = 8;
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
// Movement
if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || rb.velocity.x > 0.01f))
{
moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
}
else
{
if (isGrounded || rb.velocity.magnitude < 0.01f)
{
moveDirection = 0;
}
}
if (Input.GetKeyDown(KeyCode.W) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
//Defense
if (Input.GetKey(KeyCode.B))
{
anim.SetBool("isBlocking", true);
maxSpeed = 0f;
jumpHeight = 0f;
}
else
{
anim.SetBool("isBlocking", false);
maxSpeed = 10f;
jumpHeight = 45f;
}
//Attacks
//Simple Freeze
if (Input.GetKey(KeyCode.F))
{
anim.SetBool("isBlasting", true);
}
else
{
anim.SetBool("isBlasting", false);
}
}
void FixedUpdate()
{
Bounds colliderBounds = mainCollider.bounds;
Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, 0.1f, 0);
isGrounded = Physics2D.OverlapCircle(groundCheckPos, 0.23f, layerMask);
rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);
}
}
If any other information is needed, I'll be happy to provide.
Answer by RevivingSociety · Aug 23, 2020 at 01:28 PM
You could try to use and Int that increments when S is pressed, then on the last weapon in order, If the weaponInt is = to 3, set weaponInt to 0 or 1, however you're approaching this logic
This way the number increments up each time you press S, then it resets when it hits the final weapon, you can even make the int public so if you want to add more weapons, you can just change the amount of times it increments before it looks
I'm not the best coder but maybe it could look something like this?
private Animator anim;
private int weaponInt;
public int totalWeapons
void Start()
{
weaponInt = 0;
void Update()
{
//Summon stuff
if (Input.GetKey(KeyCode.S) && weaponInt <= totalWeapons)
{
weaponInt += 1
SwitchWeapon(weaponInt);
}
else
{
weaponInt = 1
SwitchWeapon(weaponint);
}
}
void SwitchWeapon(int weaponInt) {
switch(weaponInt)
{
case: 1
anim.SetBool("switchToScythe", false); //Do you have a false statement for the Scythe?
anim.SetBool("summonStaff", true);
break;
case: 2
anim.SetBool("summonStaff", false);
anim.SetBool("switchToScythe", true);
break;
default: //In case something breaks
anim.SetBool("switchToScythe", false);
anim.SetBool("summonStaff", true);
}
}
This utilizes a switch statement in a seperate function so that way you can pass which weaponInt is currently on and compares it to the total number of weapons you can have, if you'll always have afixed amount of weapons you can just set the totalWeapons to that amount. There's also a default in case something breaks
Again not the best coder but this is the ballpark of what came to me, I can't quite guarantee this will work, someone could improve on this if this is even the best way to handle it
Unfortunately, this didn't quite work for, but I appreciate your efforts. I'm not really sure what the problem is, though.
Answer by RufusHolton · Aug 23, 2020 at 07:43 PM
u need a cycle through system which RevivingSociety has made
the idea is that when u press the button it adds 1 to an integer and when the integer reaches max then it should reset to 0 to start again
then all u need to check is the value of the integer
for example:
Int cycle = 0;
Int max = 3;
void Update()
{
if(Input.GetKeyDown(your_button))
{
//plus 1 to cycle
cycle++
}
if(cycle >= max)
{
//reset cycle
cycle = 0
}
if(cycle == 0)
{
//code to equip weapon 0
}
else if(cycle == 1)
{
//code to equip weapon 1
}
else if(cycle == 2)
{
//code to equip weapon 2
}
//etc. (but u will have to change the max)
}
I'm not sure what I'm doing wrong, but that didn't seem to work either.
that should work, what is happening when u run it?
Your answer
Follow this Question
Related Questions
Weapon switching problem 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I make my player attack enemies without getting errors? 0 Answers
2D platformer- getting errors I don't understand (c#) 1 Answer