Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How can I fix this 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? (IDK how to properly copy paste my code)


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;

       }

   }

}

Answers

  • PBIXPBIX Member
    ```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;
            }
        }
    }
    ```
    
  • PBIXPBIX Member

    here's a better look at the code

Sign In or Register to comment.