using UnityEngine;
using System.Collections;
public class CameraFlow : MonoBehaviour
{
public Transform target;
private Vector3 offset;
// Use this for initialization
void Start()
{
offset = target.position - this.transform.position;
}
// Update is called once per frame
void Update()
{
this.transform.position = target.position - offset;
}
}1234567891011121314151617181920
using UnityEngine;
using System.Collections;
public class CameriaTrack : MonoBehaviour {
private Vector3 offset = new Vector3(0,5,4);//相机相对于玩家的位置
private Transform target;
private Vector3 pos;
public float speed = 2;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
pos = target.position + offset;
this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//调整相机与玩家之间的距离
Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//获取旋转角度
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);
}
}
1234567891011121314151617181920212223242526
using UnityEngine;
using System.Collections;
//相机一直拍摄主角的后背
public class CameraFlow : MonoBehaviour {
public Transform target;
public float distanceUp=15f;
public float distanceAway = 10f;
public float smooth = 2f;//位置平滑移动值
public float camDepthSmooth = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// 鼠标轴控制相机的远近
if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
{
Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
}
}
void LateUpdate()
{
//相机的位置
Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
//相机的角度
transform.LookAt(target.position);
}
}
using UnityEngine;
using System.Collections;
public class CameraFlow : MonoBehaviour
{
public Transform target;
Vector3 offset;
// Use this for initialization
void Start()
{
offset = transform.position - target.position;
}
// Update is called once per frame
void Update()
{
transform.position = target.position + offset;
Rotate();
Scale();
}
//缩放
private void Scale()
{
float dis = offset.magnitude;
dis += Input.GetAxis("Mouse ScrollWheel") * 5;
Debug.Log("dis=" + dis);
if (dis < 10 || dis > 40)
{
return;
}
offset = offset.normalized * dis;
}
//左右上下移动
private void Rotate()
{
if (Input.GetMouseButton(1))
{
Vector3 pos = transform.position;
Vector3 rot = transform.eulerAngles;
//围绕原点旋转,也可以将Vector3.zero改为 target.position,就是围绕观察对象旋转
transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
float x = transform.eulerAngles.x;
float y = transform.eulerAngles.y;
Debug.Log("x=" + x);
Debug.Log("y=" + y);
//控制移动范围
if (x < 20 || x > 45 || y < 0 || y > 40)
{
transform.position = pos;
transform.eulerAngles = rot;
}
// 更新相对差值
offset = transform.position - target.position;
}
}
}
---------------------
作者:尘世喧嚣
来源:CSDN
原文:https://blog.csdn.net/u011484013/article/details/51554745
版权声明:本文为博主原创文章,转载请附上博文链接!