using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControllerTest : MonoBehaviour
{
public float xOffset = 0;
public float yOffset = 0;
public float turnLRSpeed = 10;
public float turnUDSpeed = 5;
public GameObject playerObj;
public GameObject cameraObj;
bool isRotate = false;
public float invokeTime = 2;
public float cameraAngleLimit = 30;
public float currentEluerAngle_X;
void Start()
{
Invoke("SetIsRotate", invokeTime);
}
// Update is called once per frame
void Update()
{
if (isRotate)
{
xOffset = Input.GetAxis("Mouse X");//控制左右转动,左右转动,要去调整Player的旋转
yOffset = Input.GetAxis("Mouse Y");//控制上下转动,上下转动,要去调整Camera的旋转
if (playerObj != null)
{
Vector3 playerY_Rotate = new Vector3(0, 1, 0) * xOffset * turnLRSpeed * Time.deltaTime;
playerObj.transform.Rotate(playerY_Rotate);
}
if (cameraObj != null)
{
Vector3 cameraX_Rotate = -new Vector3(1, 0, 0) * yOffset * turnUDSpeed * Time.deltaTime;
print(cameraObj.transform.right);
cameraObj.transform.Rotate(cameraX_Rotate);
//限制角度
//通过欧拉角获取Transform的角度
//print("camera"+cameraObj.transform.localRotation.x);
//print(cameraObj.transform.localEulerAngles.x);
currentEluerAngle_X = cameraObj.transform.localEulerAngles.x;
if (currentEluerAngle_X <=180 && currentEluerAngle_X > cameraAngleLimit)
{
currentEluerAngle_X = cameraAngleLimit;
cameraObj.transform.localEulerAngles = new Vector3(currentEluerAngle_X, cameraObj.transform.localEulerAngles.y, cameraObj.transform.localEulerAngles.z);
}
if (currentEluerAngle_X > 180 && currentEluerAngle_X<360-cameraAngleLimit)
{
currentEluerAngle_X = 360 - cameraAngleLimit;
cameraObj.transform.localEulerAngles = new Vector3(currentEluerAngle_X,cameraObj.transform.localEulerAngles.y,cameraObj.transform.localEulerAngles.z);
}
}
}
}
void SetIsRotate()
{
isRotate = true;
}
}