I’ve got a basic movement script going but I can’t figure out how to use Mathf.Clamp so that the player can’t do flips with the camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(CharacterController))]
public class PlayerMove : MonoBehaviour {
public float playerSpeed = 2f;
public float mouseSens = 5f;
[SerializeField]
private CharacterController controller;
[SerializeField]
private Camera cam;
private float moveVertical;
private float moveHorizontal;
private float rotationY;
private float rotationX;
private float maxCameraRotation = 10f;
void Start ()
{
controller = GetComponent <CharacterController> ();
}
void Update ()
{
moveVertical = Input.GetAxis ("Vertical") * playerSpeed;
moveHorizontal = Input.GetAxis ("Horizontal") * playerSpeed;
rotationY = Input.GetAxis ("Mouse X") * mouseSens;
rotationX = Input.GetAxis ("Mouse Y") * mouseSens;
Vector3 movement = new Vector3 (moveHorizontal, 0f, moveVertical);
transform.Rotate (0f, rotationY, 0f);
cam.transform.Rotate (-Mathf.Clamp (rotationX, -maxCameraRotation, maxCameraRotation), 0f, 0f);
movement = transform.rotation * movement;
controller.Move (movement * Time.deltaTime);
}
}
That’s the whole script, including my current attempt. Help would be appreciated!