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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Interactions;
using UnityEngine.InputSystem;
// Use action set asset instead of lose InputActions directly on component.
public class SimpleMovementController : MonoBehaviour
{
public float moveSpeed;
//public float rotateSpeed;
private PlayerInput playerInput;
public void Awake()
{
playerInput = new PlayerInput();
}
public void OnEnable()
{
playerInput.Enable();
}
public void OnDisable()
{
playerInput.Disable();
}
public void Update()
{
var move = playerInput.CharacterControls.Move.ReadValue<Vector2>();
// Update orientation first, then move. Otherwise move orientation will lag
// behind by one frame.
Move(move);
}
private void Move(Vector2 direction)
{
if (direction.sqrMagnitude < 0.01)
return;
var scaledMoveSpeed = moveSpeed * Time.deltaTime;
var move = Quaternion.Euler(0, transform.eulerAngles.y, 0) * new Vector3(direction.x, 0, direction.y);
transform.position += move * scaledMoveSpeed;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Interactions;
using UnityEngine.InputSystem;
// Use action set asset instead of lose InputActions directly on component.
public class SimpleLookController : MonoBehaviour
{
public float rotateSpeed;
public Transform playerBody;
private PlayerInput playerInput;
private Vector2 m_Rotation;
public void Awake()
{
playerInput = new PlayerInput();
}
public void OnEnable()
{
playerInput.Enable();
}
public void OnDisable()
{
playerInput.Disable();
}
public void Update()
{
var look = playerInput.CharacterControls.Look.ReadValue<Vector2>();
Look(look);
}
private void Look(Vector2 rotate)
{
if (rotate.sqrMagnitude < 0.01)
return;
var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
m_Rotation.y += rotate.x * scaledRotateSpeed;
m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89);
transform.localEulerAngles = m_Rotation;
}
}