Hi everyone,
I’ve decided to use the new version of Unity’s Input System that utilizes Input Actions and Action Maps for my current game project and ran into a problem with my Inputs triggering twice or even multiple times with one buttons press. Although I can move my Character and perform the different actions with the buttons I’ve assigned, the functions that are automatically generated and needed for the Input Actions and the Interface are triggering multiple times on a button press (I’ve checked with breakpoints and Console outputs). I have tried different things like assigning Interactions to the different Actions in the Input Action interface, but the inputs still triggered multiple times.
Here’s the code I am currently using, I’ve also included a picture of the Input Action Manager with the currently assigned buttons:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class ActionMapPC : MonoBehaviour, GeneralInput.IPlayerInputActions
{
private GeneralInput controls;
[SerializeField]
private ActionMapCC actionMapCC;
private CharacterController charController;
Animator animator;
[SerializeField]
private float walkSpeed = 2;
[SerializeField]
private float runSpeed = 6;
bool running;
private Vector2 input;
float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
float speedSmoothTime = 0.2f;
float speedSmoothVelocity;
float currentSpeed;
[SerializeField]
private float mass = 80;
private void Awake()
{
controls = new GeneralInput();
controls.PlayerInput.SetCallbacks(this);
charController = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
private void OnEnable()
{
controls.PlayerInput.Enable();
}
private void OnDisable()
{
controls.PlayerInput.Disable();
}
public void OnMovement(InputAction.CallbackContext context)
{
input = context.ReadValue<Vector2>();
Debug.Log("trigger");
}
public void OnRun(InputAction.CallbackContext context)
{
float value = context.ReadValue<float>();
if (value == 1)
{
running = true;
}
else
{
running = false;
}
}
void Update()
{
Vector2 inputDir = input.normalized;
Vector3 inputDirWS = actionMapCC.MapInputToWorldSpace(inputDir);
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2(inputDirWS.x, inputDirWS.z) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
charController.SimpleMove(transform.forward * currentSpeed);
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.collider.attachedRigidbody == null)
return;
Vector3 moveDir = hit.moveDirection;
moveDir *= currentSpeed * mass;
Vector3 point = hit.point;
hit.collider.attachedRigidbody.AddForceAtPosition(moveDir, point, ForceMode.Force);
}
}
For clarification the functions OnMovement() and OnRun() are triggering multiple times, these are functions generated by the Input Action Manager. This problem is also happening in other scripts where I use this type of Input.
I hope I described my problem properly, I would be grateful if someone could help me to solve my problem.