eneeigriega
eneeigriega

Reputation: 95

Object won't stop moving. Using New Input System

I've been going through the docs and anything related to the new input system. I understand it's fairly new and there were a lot of changes on 1.0.0.

I'm just starting out in Unity after a long break and to start I've been trying to move a Player around. I got it to work but it doesn't stop after releasing the keyboard key.

I didn't change anything from the default InputActions settings at first.

Default InputActions Settings

I did try changing the Interactions to Press > Press & Release and I got it woking but if I pressed the right key, for example, and kept it pressed while changing keys (direction) it kept moving in the right direction. I quit this because it said to just use 'Button' action type but it changes the whole setup for WASD.

InputActions Settings with Press & Release

And this is my player script. It might not have anything to do with the input system and everything to do with my code:

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

public class PlayerBehaviour : MonoBehaviour 
{
    private InputActions _controls;
    private Vector2 movementInput;
    public float _speed = 12f;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void OnEnable()
    {
        _controls = new InputActions();
        _controls.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
        _controls.Player.Move.Enable();
    }

    void Update()
    {
        Debug.Log("transform.position: " + transform.position);
        transform.position += new Vector3(movementInput.x * _speed * Time.deltaTime,
                                        0,
                                        movementInput.y * _speed * Time.deltaTime);
    }

    private void OnDisable()
    {
        _controls.Player.Move.Disable();
    }
}

Update: I've been working on this and added more but couldn't get past my original issue.

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

public class PlayerMovement : MonoBehaviour
{
    private InputActions _controls;

    private Vector2 movementInput;
    public float _speed = 12f;

    public CharacterController controller;
    public Camera playerCamera;
    private Vector2 lookPosition;
    private float mouseSensitivity = 50f;
    float xRotation = 0f;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void OnEnable()
    {
        _controls = new InputActions();
        _controls.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
        _controls.Player.Move.Enable();
        _controls.Player.Look.performed += ctx => lookPosition = ctx.ReadValue<Vector2>();
        _controls.Player.Look.Enable();
    }

    void Update()
    {
        float x = movementInput.x;
        float z = movementInput.y;
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * _speed * Time.deltaTime);

        float lookX = lookPosition.x * mouseSensitivity * Time.deltaTime;
        float lookY = lookPosition.y * mouseSensitivity * Time.deltaTime;

        xRotation -= lookPosition.y;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        transform.Rotate(Vector3.up * lookX);
    }

    private void OnDisable()
    {
        _controls.Player.Move.Disable();
        _controls.Player.Look.Disable();
    }
}

Upvotes: 3

Views: 5287

Answers (4)

Dylan.S
Dylan.S

Reputation: 1

you need the cancel as your vector is being set but not reset!

Upvotes: 0

Siddharth
Siddharth

Reputation: 1

There is a simple fix for this. Just remove press and release, Set action type to value and control type to any. If required(Which I highly doubt) change a few lines in your script to support value.

Upvotes: 0

spiritworld
spiritworld

Reputation: 143

I had this very same problem. Previously I was using PlayerInput with UnityEvents and the WASD Move worked just fine. Later I wanted to change into C# events and it was like your code: using only performed delegate.

Digging into PlayerInput source I noticed that the UnityEvents register in all three started/performed/canceled delegates.

Solution:

_controls.Player.Move.started += ctx => movementInput = ctx.ReadValue<Vector2>()
_controls.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>()
_controls.Player.Move.canceled += ctx => movementInput = ctx.ReadValue<Vector2>()

Also no need to add Press action, it works without it.

Upvotes: 3

eneeigriega
eneeigriega

Reputation: 95

So yeah as I mentioned I got it to work adding the Press Interaction.

Move Action -> WASD Binding -> "+" in Interactions and Press. Then setting Trigger Behavior to Press And Release.

I drove away from this since if I press my way through multiple WASD keys it keeps moving in the direction of the first key pressed. I'll look this up separately.

Upvotes: 1

Related Questions