- Home /
Player won't stop moving while blocking
In my script, I have it so that when the player presses 'B' on the keyboard the in-game character blocks an attack. When I first implemented the mechanic using script, the player could move while blocking which was something I didn't want, so I fixed it by turning the movement and jump to 0 in the instance that the player was pressing 'B'. And that worked just fine until now. For some reason after I had added another mechanic that allows the player to shoot projectiles, the player is now able to move while blocking, even though the script is telling them not to. The problem really only lies with the left-right movement since the player isn't able to jump.
I've tried increasing the gravity and lowering the movement passed 0 =, but to no avail.
Here's the script that contains the player's blocking and projectile mechanic:
{
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 = -100f;
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);
}
}
Not sure if it's needed, but here's an additional script for the projectile:
{ public Rigidbody2D iceBlast; public float iceBlastSpeed;
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
StartCoroutine(Wait());
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds(.3f);
var iceBlastInst = Instantiate(iceBlast, transform.position, Quaternion.Euler(new Vector2(0, 0)));
iceBlastInst.velocity = new Vector2(iceBlastSpeed, 0);
}
}
Feel free to ask questions, and thank you for the help.
For reference, I'd like to note that your character specifically moves when blocking in response to these lines:
// ...
maxSpeed = -100f;
// ...
and
// ...
rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);
// ...
Without any other handling, your movement speed is multiplied by your maximum speed, regardless of whether it's positive or negative. You can solve the problem by setting maxSpeed to 0f when blocking, but it might help to research more and find a more thorough solution.
Answer by Callum_0 · Aug 21, 2020 at 07:18 PM
Have you tried to use a bool to specify if the player can move?
private bool _canMove = false;
Then just set the bool where your blocking code is:
if (Input.GetKey(KeyCode.B))
{
anim.SetBool("isBlocking", true);
_canMove = false;
}
else
{
anim.SetBool("isBlocking", false);
_canMove = true;
}
And in your movement code just check if _canMove is true.
if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || rb.velocity.x > 0.01f) && _canMove)
{
moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
}
else
{
if (isGrounded || rb.velocity.magnitude < 0.01f)
{
moveDirection = 0;
}
}
Thank you for the reply, but that didn't seem to work, unfortunately.
Change this:
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;
}
}
To this:
if (isGrounded || rb.velocity.x > 0.01f)
{
if (Input.GetKey(KeyCode.A)
moveDirection = -1;
else if (Input.GetKey(KeyCode.D)
moveDirection = 1;
else
moveDirection = 0;
}
Then inside your FixedUpdate method do this:
if (_can$$anonymous$$ove)
{
rb.velocity = new Vector2((moveDirection) * maxSpeed, rb.velocity.y);
}
That's not working either. I'm not really sure what the problem is.