My dash cancels out WASD movement

This is based on BMo’s 5-minute top down shooter, I’m trying to add a dash but for some reason it cancels out the wasd movement so I can only move with the dash. There are no errors in Unity, does anyone know how to fix this?

using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Weapon Weapon;
public float dashSpeed = 200000;

public Rigidbody2D rg;

bool dash = true;
int dashCooldown = 80;

Vector2 moveDirection;
Vector2 mousePosition;

void Start()
{

}

void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");

if(Input.GetMouseButtonDown(0))
{
Weapon.Fire();
}

if(Input.GetKeyDown(KeyCode.R))
{
Weapon.Reload();
}

moveDirection = new Vector2(moveX, moveY).normalized;
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

}

private void FixedUpdate()
{
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);

Vector2 aimDirection = mousePosition - rb.position;
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = aimAngle;

if (dashCooldown == 0)
{
dash = true;
}
else
{
dashCooldown--;
}

rg.velocity = Vector2.zero;

if (dash && Input.GetKey(KeyCode.Q))
{
Vector2 mouseDirection = (Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2)).normalized;
rg.AddForce(mouseDirection * dashSpeed * Time.fixedDeltaTime);
dash = false;
dashCooldown = 80;
}
}
}

Generally you can either move under player control, OR you can be in a “dashing” or “dodging” state for some amount of time (or distance).

Obviously if you need both then you need to code the ability for fresh WASD input to override dashing, and vice versa.

As for what’s wrong, who knows. I cannot read unformatted code.

I see and appreciate your effort with the triple tick marks (```) but that’s not how this board works, alas. :slight_smile:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You may edit your post above.

Either way, staring at walls of code is rarely helpful. Instead… welcome to debugging!

Here is how to get started:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

You must find a way to get the information you need in order to reason about what the problem is.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Weapon Weapon;
public float dashSpeed = 200000;
public Rigidbody2D rg;
bool dash = true;
int dashCooldown = 80;
Vector2 moveDirection;
Vector2 mousePosition;
void Start()
{
}
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
if(Input.GetMouseButtonDown(0))
{
Weapon.Fire();
}
if(Input.GetKeyDown(KeyCode.R))
{
Weapon.Reload();
}
moveDirection = new Vector2(moveX, moveY).normalized;
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate()
{
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
Vector2 aimDirection = mousePosition - rb.position;
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = aimAngle;
if (dashCooldown == 0)
{
dash = true;
}
else
{
dashCooldown--;
}
rg.velocity = Vector2.zero;
if (dash && Input.GetKey(KeyCode.Q))
{
Vector2 mouseDirection = (Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2)).normalized;
rg.AddForce(mouseDirection * dashSpeed * Time.fixedDeltaTime);
dash = false;
dashCooldown = 80;
}
}
}

ok here is a better look at the code.

Thanks for your help by the way

Debug.Log() isn’t working, I end up with compiler errors

Nvm I fixed it by making two separate scripts

For future reference, you can fix compiler errors yourself. Here’s how:

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

1 Like