Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Need help solving my mouse input not working.

Discussion in 'Input System' started by omtanakorn2550, Oct 26, 2023.

  1. omtanakorn2550

    omtanakorn2550

    Joined:
    Jul 24, 2022
    Posts:
    1
    I used two seperate files one for main player control and another for rotating a camera. I get the input in the first script and send the input to the camera rotate script. But my camera didnt move at all.

    Here's the PlayerController

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private GameInput inputActions;
    9.     private Vector2 movement;
    10.     private Rigidbody rb;
    11.  
    12.     public float speed;
    13.     public float mouseSpeed;
    14.     public Vector2 cameraMovement;
    15.  
    16.     private void OnEnable()
    17.     {
    18.         inputActions.Gameplay.Movement.Enable();
    19.     }
    20.  
    21.     private void OnDisable()
    22.     {
    23.         inputActions.Gameplay.Movement.Disable();
    24.     }
    25.  
    26.     private void Awake()
    27.     {
    28.         inputActions = new GameInput();
    29.  
    30.         inputActions.Gameplay.Movement.performed += ctx => movement = ctx.ReadValue<Vector2>();
    31.         inputActions.Gameplay.Movement.canceled += ctx => movement = Vector2.zero;
    32.  
    33.         inputActions.Gameplay.Mouse.performed += ctx => cameraMovement = ctx.ReadValue<Vector2>();
    34.         inputActions.Gameplay.Mouse.canceled += ctx => cameraMovement = Vector2.zero;
    35.     }
    36.  
    37.     private void Start()
    38.     {
    39.         rb = GetComponent<Rigidbody>();
    40.     }
    41.  
    42.     private void Update()
    43.     {
    44.         rb.AddForce(Vector3.forward * movement.y * speed);
    45.         rb.AddForce(Vector3.right * movement.x * speed);
    46.     }
    47. }
    48.  
    And here's the CameraRotator


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class CameraRotate : MonoBehaviour
    7. {
    8.     private PlayerController playerController;
    9.  
    10.     private void Start()
    11.     {
    12.         playerController = FindObjectOfType<PlayerController>();
    13.     }
    14.     private void Update()
    15.     {
    16.         if (playerController != null)
    17.         {
    18.             Vector2 cameraMovement = playerController.cameraMovement;
    19.             float mouseSpeed = playerController.mouseSpeed;
    20.             transform.Rotate(Vector3.up * cameraMovement * mouseSpeed * Time.deltaTime); // CameraMovement bug
    21.             Debug.Log("Import complete!");
    22.         }
    23.     }
    24. }
    25.  
    In the second script, when i remove cameraMovement variable, the camera just works again, but without player input obviously. If somebody got a solution please reply this thread. Thanks!