Question by
Jorvan · Jun 18, 2016 at 01:07 AM ·
scripting problemcamera rotate
Need help limiting the x rotation of my camera and internet didn't helped me :(
Hi, I have been trying to fix it all day long, I searched in everywhere, but nothing worked :/ My problem is that I have my player with a cam attached to it and the rotations are fine, but want to limit the rotation of the x axis in the camera between -15 and 15, do you know how can I do it? D:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
private Animator anim;
private CharacterController controller;
public float speed = 6.0f;
public float turnSpeed = 60.0f;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20.0f;
public Transform camTransform;
//(...)
//Camera
public float Y_ANGLE_MIN = -15.0f;
public float Y_ANGLE_MAX = 15.0f;
private float turnx = 0.0f;
private float turnyc = 0.0f;
void Update (){
//Animations
(...)
//Moving
(...)
//Rotations
turnx = Input.GetAxis ("Mouse X");
turnyc = Input.GetAxis ("Mouse Y");
transform.Rotate(0, turnx * turnSpeed * Time.deltaTime, 0);
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
camTransform.transform.Rotate (turnyc * turnSpeed * Time.deltaTime * -1, 0, 0);
}
}
Comment
Best Answer
Answer by Jorvan · Jun 18, 2016 at 09:37 PM
Great news: I SOLVED IT
I will share with you the correction.
Instead of Y_ANGLE_MIN and Y_ANGLE_MAX, I used:
private float rotcam = 0; public float rotcamax = 15f; public float rotcamin = -15f;
And in the Rotation portion I wrote this:
turnx = Input.GetAxis ("Mouse X");
turnyc = Input.GetAxis ("Mouse Y");
transform.Rotate(0, turnx * turnSpeed * Time.deltaTime, 0);
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
rotcam += turnyc * turnSpeed * Time.deltaTime * tutu;
rotcam = Mathf.Clamp (rotcam, rotcamin, rotcamax);
camTransform.eulerAngles = new Vector3(rotcam, camTransform.eulerAngles.y);
Hope it helps someone :) Take care
Your answer