Unity学习之二:相机跟随

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
//距离
public float distance = 15;
//横向角度
public float rot = 0;
//纵向角度
private float roll = 30f * Mathf.PI * 2 /360;
//目标物体
private GameObject target;
// Start is called before the first frame update
void Start()
{
target = GameObject.Find("Cube");
}
// Update is called once per frame
void Update()
{
}
void LateUpdate()
{
if(target == null)
{
return;
}
if(Camera.main == null)
{
return;
}
//目标的坐标
Vector3 targetPos = target.transform.position;
Vector3 cameraPos;
float d = distance * Mathf.Cos(roll);
float height = distance * Mathf.Sin(roll);
cameraPos.x = targetPos.x + d*Mathf.Cos(rot);
cameraPos.z = targetPos.z + d*Mathf.Sin(rot);
cameraPos.y = targetPos.y + height;
Camera.main.transform.LookAt(target.transform);
}
}

浙公网安备 33010602011771号