Hi. I am trying to use new input system. And I found weird thing.
I wanted to control my camera without moving my player.
Here is the code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class FollowCamera : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float cameraSpeed = 10f;
bool isPlayerTouchKeyBoard = false;
bool ishold = false;
Vector3 playerOriginalPosition;
Vector3 movement;
private void Start()
{
}
void Update()
{
if(isPlayerTouchKeyBoard)
{
transform.position += new Vector3(movement.x * Time.deltaTime * cameraSpeed, 0, movement.y * Time.deltaTime * cameraSpeed);
}
else if(!isPlayerTouchKeyBoard)
{
// FollowPlayer();
}
}
public void OnMove(InputAction.CallbackContext context)
{
isPlayerTouchKeyBoard = true;
Vector2 input = context.ReadValue<Vector2>();
if(input != null)
{
if(context.performed)
{
ishold = true;
}
else if(context.canceled)
{
ishold = false;
}
Debug.Log(input);
Debug.Log(transform.position);
movement = input;
}
}
public void ReturnToPlayer(InputAction.CallbackContext context)
{
if(context.ReadValueAsButton())
{
transform.position = target.position;
isPlayerTouchKeyBoard = false;
}
}
private void FollowPlayer()
{
if (target == null) return;
transform.position = target.position;
}
}
What is werid is that if I change the hierarchy on the new input system’s event, I can see that OnMove is invoked or not depends on that.
And I get the error during the runtime if I try to move the camera
InvalidOperationException while executing ‘performed’ callbacks of ‘CameraMove/Move[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d,/Keyboard/upArrow,/Keyboard/downArrow,/Keyboard/leftArrow,/Keyboard/rightArrow]’
UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr)
This is the error.
When OnMove function is more upper level than ReturnToPlayer, camera move with the error.
On the opposite situation, camera does not move with the error.
I guess ReturnToPlayer invoked somehow even though I did not press space bar as I binded on the new input system.
Does anyone know how to solve it? I get stuck for 5 hours.