Unity的脚本发现自己还真是薄弱哇,现在还有一个问题没有解决,就是摄像机旋转时,不能低于地表 ,我考虑是可以用本地坐标的欧拉角y的绝对值小于90度,但是迫于好多东西不是很懂,只能放弃先,有会得小伙伴可以留言哦,下面帖一下代
码’图就不上了,靴靴
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Third_Camera_Controller : MonoBehaviour
{
public float speed=10;//镜头旋转的速度
private float _distance_away_player = 4;
private float _distance_away_ground = 2;
private float _smooth = 1;
private bool _change_away = false;
private bool _change_height = false;
private GameObject player;
public GameObject temp;
private bool _Lock_Camera_Rot=false;//判断是否锁死摄像头视角
private float _x=0f;
private float _y=10f;
private bool _change_distance = false;
private float distance = 5;
private Vector3 _Carmera_Target_Position;
// Use this for initialization
void Start()
{
player = GameObject.FindWithTag("Player");
}
Quaternion qt;
Vector3 dirction;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.P)){
_change_distance = !_change_distance;
}//控制是否锁屏的bool变化
if (_change_distance)
{
distance += Input.GetAxis("Mouse ScrollWheel") * 5f;
}
distance = Mathf.Clamp(distance, 3, 7);//更改距离大小,并限定变化范围
if (Input.GetKey(KeyCode.O))
{
_Lock_Camera_Rot = !_Lock_Camera_Rot;
}//管理是否锁死镜头bool
if (!_Lock_Camera_Rot)
{
_x += Input.GetAxis("Mouse X") * speed * Time.deltaTime;
_y += Input.GetAxis("Mouse Y") * speed * Time.deltaTime;
_y = Mathf.Clamp(_y, 100f, 1000000000f);
print(_y);
}//改变旋转角度,xyzhou的
qt = Quaternion.Euler(-_y, -_x, 0);//得到四元组
dirction = qt * Vector3.forward;//得到旋转角度
_Carmera_Target_Position = player.transform.position + dirction * distance;//得到当前镜头位置
// transform.position = Vector3.Lerp(transform.position, _Carmera_Target_Position, Time.deltaTime * _smooth);//差值旋转到目标位置
transform.LookAt(temp.transform);//镜头始终看向player
}
}
码’图就不上了,靴靴