Unable to move North West whilst Firing

Whilst holding down spacebar (Fire1) I can move in all directions except North West (Up + Left) with arrow keys (W + A works).

I can move NW and not fire or fire and not move NW (I can move SE, SW ect…)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
[SerializeField] float moveSpeed = 4f; //To be multiplied by deltaTime to increase/decrease movement speed
[SerializeField] float xPadding = 0.3f; //adds a value to the x axis to compensate for sprite width/length
[SerializeField] float yPadding = 0.25f; //adds a value to the y axis to compensate for sprite width/length
[SerializeField] float projectileSpeed = 10f;
[SerializeField] float projectileFiringPeriod = 0.1f;
[SerializeField] GameObject playerProjectile;

Coroutine firingCoroutine;

float xMin; //Furthest west the player can move
float xMax; //Furthest east the player can move
float yMin; //Furthest south the player can move
float yMax; //Furthest north the player can move

// Start is called before the first frame update
void Start()
{
    SetUpMoveBoundaries();       
}

// Update is called once per frame
void Update()
{
    Move();
    Fire();
}    

private void SetUpMoveBoundaries() //defines where the player can move within the scene
{
    Camera gameCamera = Camera.main;
    xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + xPadding; //converts screen space values to relative view space (bottom left 0,0 & top right 1,1) and assigns xMin a value within that space
    xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - xPadding;
    yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + yPadding;
    yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - yPadding;
}

private void Move() //gives instruction on how to move the player
{
    var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; //Get movement direction from Input Managers Axes' "Horizontal" field multiplied by The completion time in seconds since the last frame (Time.deltaTime - Equalises movement speed between fast and slow FPS)
    var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

    var newXPos = Mathf.Clamp(transform.position.x, xMin, xMax) + deltaX; //new X position = Current position of X + Movement direction
    var newYPos = Mathf.Clamp(transform.position.y, yMin, yMax) + deltaY;

    if (!Input.GetButton("Horizontal") & (!Input.GetButton("Vertical"))) //If move buttons aren't being pressed
    {
        transform.position = new Vector2(transform.position.x, transform.position.y); //Stay in the same position (removes decceleration)
    }
    else //If a move button is being pressed*/
    {
        transform.position = new Vector2(newXPos, newYPos); //New position = new X position + new Y position
    }        
}

private void Fire()
{
    if (Input.GetButtonDown("Fire1")) //When positive button is pressed within "Fire1" of Input                   
    {
       firingCoroutine = StartCoroutine(FireContinuously()); //Set a variable as StartCoroutine(FireContinuously()) and execute     
    }        
    if (Input.GetButtonUp("Fire1")) //When positive button is released
    {
        StopCoroutine(firingCoroutine); //Stop the variable Coroutine "firingCoroutine"
    }
}

IEnumerator FireContinuously() //Coroutine
{
    while (true) //Once "Fire1" is pressed FireContinuously becomes true (Wouldnt become false after releasing without a StopCoroutine) . Whilst true execute following code.
    { 
    var projectile = Instantiate(playerProjectile, transform.position, Quaternion.identity)/* as GameObject*/; //Instantiate prefab at player location
    projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed); //Increase prefabs (Rigidbody2D's) velcocity by "projectileSpeed"        
    yield return new WaitForSeconds(projectileFiringPeriod); //Waits for projectileFiringPeriod (0.1f) then executes Coroutine again (Due to while true)
    }
}

}

Is there a solution to this?

Hi Jacob,

Welcome to our forum. :slight_smile:

The issue might be caused by the keyboard ghosting effect, which is an inherent problem of your hardware (and the hardware of the majority of people). Please test the key combinations on this website to see if the effect in your game is caused by your keyboard.

1 Like

Yes thats it! You’re doing Gods work thank you.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms