Hello,
I’m a beginner at Unity, and am attempting to make a 2D Platform game. I’m making the Attacking Script for the player, but the problem is, I don’t want the character to move at all when the player hits the attack button. As an example, I’ve set it up something like this:
The PlayerController script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Rigidbody2D rb;
public Animator anim;
public float speed;
public float jumpHeight;
public Transform groundCheck;
public LayerMask whatIsGround;
public AttackController attackController;
private bool stab;
private bool upSwing;
private bool downSwing;
private bool sideSwing;
private bool guarding;
float groundRadius = 0.2f;
public bool grounded = false;
bool facingRight;
void Start () {
rb = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
facingRight = true;
attackController = GameObject.Find ("Player").GetComponent<AttackController>();
}
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
stab = attackController.stab;
upSwing = attackController.upSwing;
downSwing = attackController.downSwing;
sideSwing = attackController.sideSwing;
if (!sideSwing) {
if (move != 0 && grounded) {
rb.velocity = new Vector2 (move * speed, rb.velocity.y);
anim.SetBool ("IsWalking", true);
} else {
anim.SetBool ("IsWalking", false);
}
}
And the AttackController script:
using UnityEngine;
using System.Collections;
public class AttackController : MonoBehaviour {
public Animator anim;
public Rigidbody2D rb;
public bool stab;
public bool upSwing;
public bool downSwing;
public bool sideSwing;
public PlayerController playerController;
private bool OnGround;
void Start () {
anim = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
playerController = GameObject.Find ("Player").GetComponent<PlayerController>();
}
void Update () {
if (Input.GetKeyDown (KeyCode.X) && (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.LeftArrow))&& OnGround) {
anim.SetTrigger ("SideSwing");
sideSwing = true;
Debug.Log ("Player is SideSwing");
} else {
sideSwing = false;
}
Now the PlayerController script can access the sideSwing bool from AttackController script, so that when sideSwing is true, the movement code will not run. The problem is, when the player has GetKeyDown on the X key, the bool is only true for a single FixedUpdate frame, and can still move while the animation plays from the animator.
Changing it to just GetKey will work, but only if the player is holding down the key, and that’s unnecessary in a platformer. I’ve been considering putting the bool in some kind of loop so that it stays true for a limited time, like one second or even the animation clip. But because I’m using an Animator I don’t know how to get the length of the current animation being played. I also don’t know how to make the bool stay in a time loop without using coroutines, and I’m not sure how to implement that.
Perhaps there’s a simpler way to do this, and I’m not seeing it.