Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Have FPS Camera Control Body Rotation

Discussion in 'Input System' started by jthiess, Oct 18, 2021.

  1. jthiess

    jthiess

    Joined:
    Nov 5, 2018
    Posts:
    31
    Hello, I am trying to make a quick prefab using the new Input System. I want a simply Capsule Body with a Cube head + Camera. Body moves forward, backward, left and right, and the Cube + Camera look up and down and turn the body left and right. I can't seem to get it! I have created a Look script (attached to camera) and a Move script (attached to body) based off the new Input System samples. They both work as intended, but I don't know how to get the camera's x rotation to rotate the body.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem.Interactions;
    5. using UnityEngine.InputSystem;
    6.  
    7. // Use action set asset instead of lose InputActions directly on component.
    8. public class SimpleMovementController : MonoBehaviour
    9. {
    10.     public float moveSpeed;
    11.     //public float rotateSpeed;
    12.     private PlayerInput playerInput;
    13.  
    14.     public void Awake()
    15.     {
    16.         playerInput = new PlayerInput();
    17.  
    18.     }
    19.  
    20.     public void OnEnable()
    21.     {
    22.         playerInput.Enable();
    23.     }
    24.  
    25.     public void OnDisable()
    26.     {
    27.         playerInput.Disable();
    28.     }
    29.  
    30.     public void Update()
    31.     {
    32.  
    33.         var move = playerInput.CharacterControls.Move.ReadValue<Vector2>();
    34.  
    35.         // Update orientation first, then move. Otherwise move orientation will lag
    36.         // behind by one frame.
    37.         Move(move);
    38.     }
    39.  
    40.     private void Move(Vector2 direction)
    41.     {
    42.         if (direction.sqrMagnitude < 0.01)
    43.             return;
    44.         var scaledMoveSpeed = moveSpeed * Time.deltaTime;
    45.         var move = Quaternion.Euler(0, transform.eulerAngles.y, 0) * new Vector3(direction.x, 0, direction.y);
    46.         transform.position += move * scaledMoveSpeed;
    47.     }
    48. }
    49.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem.Interactions;
    5. using UnityEngine.InputSystem;
    6.  
    7. // Use action set asset instead of lose InputActions directly on component.
    8. public class SimpleLookController : MonoBehaviour
    9. {
    10.     public float rotateSpeed;
    11.  
    12.     public Transform playerBody;
    13.  
    14.     private PlayerInput playerInput;
    15.     private Vector2 m_Rotation;
    16.  
    17.     public void Awake()
    18.     {
    19.         playerInput = new PlayerInput();
    20.     }
    21.  
    22.     public void OnEnable()
    23.     {
    24.         playerInput.Enable();
    25.     }
    26.  
    27.     public void OnDisable()
    28.     {
    29.         playerInput.Disable();
    30.     }
    31.  
    32.     public void Update()
    33.     {
    34.         var look = playerInput.CharacterControls.Look.ReadValue<Vector2>();
    35.  
    36.         Look(look);
    37.     }
    38.  
    39.  
    40.     private void Look(Vector2 rotate)
    41.     {
    42.         if (rotate.sqrMagnitude < 0.01)
    43.             return;
    44.         var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
    45.         m_Rotation.y += rotate.x * scaledRotateSpeed;
    46.         m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89);
    47.         transform.localEulerAngles = m_Rotation;
    48.     }
    49. }
    50.  
     
  2. jthiess

    jthiess

    Joined:
    Nov 5, 2018
    Posts:
    31
    I think I sorted it out....

    I added this to the Look script
    Code (CSharp):
    1. private void Look(Vector2 rotate)
    2.     {
    3.         if (rotate.sqrMagnitude < 0.01)
    4.             return;
    5.         var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
    6.         m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89);
    7.         transform.localEulerAngles = m_Rotation;
    8.     }
    and this to the move script
    Code (CSharp):
    1. private void Look(Vector2 rotate)
    2.     {
    3.         if (rotate.sqrMagnitude < 0.01)
    4.             return;
    5.         var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
    6.         m_Rotation.y += rotate.x * scaledRotateSpeed;
    7.         transform.localEulerAngles = m_Rotation;
    8.     }
    It seems to work, but may be redundant code?