Simple issue with the structure of C# rigidbody2D code
Hey guys! Just a simple issue I'm having here - all the code works fine except for one part. When my player character starts to attack I slow down their movement using the following:
}
if (Input.GetKey("right") && Input.GetKey("space"))
GetComponent<Rigidbody2D>().drag = 250f;
else GetComponent<Rigidbody2D>().drag = 0f;
{
The only issue is that this 'right+space' code doesn't work but the 'left+space' code below it DOES work. If I delete the 'left+space' code then the 'right+space' code starts to work! I'm an artist not a coder so I think this is probably something simple to do with the ordering or something >.< Also any tips to streamline this code would be much appreciated!
(Here's the full code for reference)
using UnityEngine;
using System.Collections;
public class KiritoController : MonoBehaviour
{
public float MaxSpeed = 10f;
Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
}
void Update()
{
//ATTACKING
{
if (Input.GetKey("right") && Input.GetKey("space"))
animator.SetBool("AttackingRight", true);
else animator.SetBool("AttackingRight", false);
}
if (Input.GetKey("right") && Input.GetKey("space"))
GetComponent<Rigidbody2D>().drag = 250f;
else GetComponent<Rigidbody2D>().drag = 0f;
{
if (Input.GetKey("left") && Input.GetKey("space"))
animator.SetBool("AttackingLeft", true);
else animator.SetBool("AttackingLeft", false);
}
{
if (Input.GetKey("left") && Input.GetKey("space"))
GetComponent<Rigidbody2D>().drag = 250f;
else GetComponent<Rigidbody2D>().drag = 0f;
}
{
if (Input.GetKey("up") && Input.GetKey("space"))
animator.SetBool("AttackingUp", true);
else animator.SetBool("AttackingUp", false);
}
{
if (Input.GetKey("down") && Input.GetKey("space"))
animator.SetBool("AttackingDown", true);
else animator.SetBool("AttackingDown", false);
}
}
}
Answer by LazyElephant · Feb 16, 2016 at 03:24 AM
The reason it's not working is because of the way you have your if/else statements set up. For both the left and right cases, if it's not true, the drag gets set back to 0. What happens when you press right+space is this:
The first and second if statements evaluate to true. The AttackingRight variable is set to true and the drag is set to 250.
The third and fourth if statements (left + space) evaluate to false. The AttackingLeft variable is set to false and the drag is set to 0.
To fix this, you need to make it so the drag variable is only set back to 0 if both of the if statements evaluate to false. Two other changes I would recommend are:
Caching the Rigidbody2D. Calls to GetComponent take time. In a single class like this it won't really be noticeable on a human time scale, but it can add up. So it's good practice to keep a reference in your class to objects you will be updating frequently.
Factor the space key out of your multiple if statements. I may be wrong, but it appears that the space key is used as your attack key and the directional keys are just there to control the direction of the attack. So, you can set up an if statement surrounding all your other statements which tests for the space key. This will allow your program to skip processing all the other if statements if the player isn't trying to attack. This also gives you a convenient place to reset your attacking animation variables.
The resulting code would be:
using UnityEngine;
using System.Collections;
public class KiritoController : MonoBehaviour
{
public float MaxSpeed = 10f;
Animator animator;
Rigidbody2D rigidbody;
void Start (){
animator = GetComponent<Animator>();
rigidbody = GetComponent<Rigidbody2D>();
}
void Update(){
//ATTACKING
if (Input.GetKey("space")){
if (Input.GetKey("right")){
animator.SetBool("AttackingRight", true);
rigidbody.drag = 250f;
}
if (Input.GetKey("left")){
animator.SetBool("AttackingLeft", true);
rigidbody.drag = 250f;
}
if (Input.GetKey("up"))
animator.SetBool("AttackingUp", true);
if (Input.GetKey("down"))
animator.SetBool("AttackingDown", true);
} else {
animator.SetBool("AttackingDown", false);
animator.SetBool("AttackingRight", false);
animator.SetBool("AttackingLeft", false);
animator.SetBool("AttackingUp", false);
rigidbody.drag = 0f;
}
}
}
There are most likely better ways to do what you want, but this should solve your problem. Good luck.
Hey that works so much nicer! And it makes sense getting the Rigidbody2D component at the start of the script rather than constantly accessing it in the Update!
I'm learning a lot through this - so thank you!