Mouse movements not working with Input.GetAxis("Horizontal");

I’m starting the Laser Defender project and when using Input.GetAxis(“Horizontal”) and the vertical equivalent I get no movement via mouse movement. The keyboard keys work but I get nothing from the mouse movement. I’ve checked the code many times but it seems right. Please help!

 private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
        var newXPos = transform.position.x + deltaX;
        var newYPos = transform.position.y + deltaY;
        transform.position = new Vector2(newXPos, newYPos);
    }

Hi and welcome to our community! :slight_smile:

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? If Move() gets called after your code processed and applied the mouse input, the code of Move() overrides the values of the mouse.

I’m following the course exactly. This is literally the first step we’ve done in code. Here’s the whole script.

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

public class Player : MonoBehaviour
{
    [SerializeField] float moveSpeed = 1f;

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

    // Update is called once per frame
    void Update()
    {

        Move();

    }

    private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
        Debug.Log(deltaX);
        var newXPos = transform.position.x + deltaX;
        var newYPos = transform.position.y + deltaY;
        transform.position = new Vector2(newXPos, newYPos);
    }
}

If under Input Manager, Horizontal I change the “type” to “Mouse Movement” instead of “Key or Mouse Button” I do get some movement with the mouse but it’s wild and the sprite is most of the time off screen and unfindable. Also that’s not what he tells you to do. Same for Vertical.

We do not use the mouse in this game, only the keyboard.

You could try to remove moveSpeed and Time.deltaTime when using the mouse. Maybe that’ll give you better results.

Thanks. I could have sworn he was saying that we were using the mouse as input and as a bonus it also works with keyboard. Maybe he was just explaining the different options and I misunderstood. At least I know I’m not crazy in regards to the mouse movement not doing anything.

Hi,

Just a guess , Have you checked the input manager for Horizontal Type is set to Mouse Movement? its default is key or mouse button.

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

Privacy & Terms