Unity中实现人物相机跟随,镜头旋转

更新时间:2023-05-14 13:49:35 阅读: 评论:0

Unity中实现⼈物相机跟随,镜头旋转
参考⾃:
挂在⼈物上的:MoveController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour {
public float speed = 6.0F; //⼈物移动速度
public float jumpSpeed = 5.0F;
public float gravity = 5.0F;
private Vector3 moveDirection = ;
public float rotatinDamping = 4;    //⼈物旋转的速度
public float mou1RotateDamping = 4;
public bool cameraIsRotate = true;      //判断相机是否跟随⼈物旋转(点击⿏标左键可观看⾓⾊)
private float backgroundRotateValue;                //点击⿏标右键,存储⿏标X⽅向位移
float currentOnClickMou1AngleY = 0;    //⿏标右击时⼈物当前的Y轴度数
boarfloat currentCameraAngleY = 0;          //⿏标左击时相机当前的Y轴度数
public GameObject cam;                  //⼈物后⾯的相机
private CharacterController characterContro;  //⾓⾊控制器组件
// U this for initialization
void Start()
{
characterContro = this.GetComponent<CharacterController>();
cam = GameObject.Find("Main Camera");
}
// Update is called once per frame
void Update()
{
move();
mouControllerRotation();
}
void move()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);英文艺术字
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
/// <summary>
/// ⿏标控制旋转
timemanagement/// </summary>
void mouControllerRotation()
{
if (Input.GetMouButtonDown(1))
{
currentOnClickMou1AngleY = transform.eulerAngles.y;
backgroundRotateValue = 0;
}
if (Input.GetMouButton(1))
{
backgroundRotateValue += Input.GetAxis("Mou X") * mou1RotateDamping;
美联英语怎么样
transform.eulerAngles = new Vector3(transform.eulerAngles.x, backgroundRotateValue + currentOnClickMou1AngleY, transform.eulerAngles.z);
}
}
}
挂在相机上的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
private MoveController mc;      //获取⼈物控制组件
Transform target;        //相机跟随的⽬标位置
public float rotationDamping = 6;        //相机跟随⼈物的旋转速度
public float zoomSpeed = 4;                //⿏标滚轮滑动速度
private float h1;                      //点击⿏标右键,存储⿏标Y⽅向位移
private float h2;                      //点击⿏标左键,存储⿏标Y⽅向位移
private float offt;                  //镜头左右旋转的差值
private float distance;    //相机和⽬标的距离
private bool cameraIsRotate;
private float oldRotationAngle;
//private float height = 1f;      //相机和⽬标的⾼度
//private float heightDamping = 1;
// Vector3 offtPosition;
// U this for initialization
void Start()
{
offt = 0;
cameraIsRotate = true;
target = GameObject.Find("Player_archer_arrow_master").transform;
oldRotationAngle = transform.eulerAngles.y;
mc = target.gameObject.GetComponent<MoveController>();
distance = Vector3.Distance(new Vector3(0, 0, target.position.z), new Vector3(0, 0, transform.position.z));
//offtPosition = target.position - transform.position;
}
// Update is called once per frame
void Update()
{
//transform.position = target.position - offtPosition;
flowTarget();
zoomView();
UpAndDownView();
LeftAndRightView();
}
/// <summary>
/// <summary>
/// 相机跟随⼈物移动旋转
/// </summary>
void flowTarget()
{
float wantedRotationAngle = oldRotationAngle;    //要达到的旋转⾓度
//float wantedHeight = target.position.y + height;    //要达到的⾼度
float currentRotationAngle = transform.eulerAngles.y; //当前的旋转⾓度
/
/float currentHeight = transform.position.y;          //当前的⾼度
if (cameraIsRotate)
{
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);        }
// currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);    //由当前⾼度达到要达到的⾼度        Quaternion currentRotation = Quaternion.Euler(transform.eulerAngles.x, currentRotationAngle, 0);
// float currentRotation=1;  //防⽌主⾓回头摄像机发⽣旋转,这⾥不⽤
Vector3 ca = target.position - currentRotation * Vector3.forward * distance;    //tt是相机的位置
// transform.position = target.position-currentRotation * Vector3.forward * distance;
transform.position = new Vector3(ca.x, transform.position.y, ca.z);      //最后得到的相机位置
// transform.LookAt(target.position);
}
/// <summary>
/// 滚轮控制缩放
/// </summary>
void zoomView()
{
float scrollWheel = Input.GetAxis("Mou ScrollWheel") * zoomSpeed;
distance -= scrollWheel;
if (distance > 5.6f)
suggest是什么意思
{
distance = 5.6f;
}
if (distance < 0.9f)
{
distance = 0.9f;
}
}
/// <summary>
bvi/// 摄像头上下视⾓
/// </summary>
void UpAndDownView()
{
津波
if (Input.GetMouButton(0))
{
Debug.Log(oldRotationAngle);
h1 = Input.GetAxis("Mou Y") * rotationDamping;
Vector3 originalPosition = transform.position;
Quaternion originalRotation = ation;
transform.RotateAround(target.position, -target.right, h1);    //决定因素position和rotation
float x = transform.eulerAngles.x;
if (x < -10 || x > 80)
{
transform.position = originalPosition;
}
}
}
/// <summary>
stephen gately/// 摄像头左右视⾓
/// </summary>
void LeftAndRightView()
留学收费void LeftAndRightView()
{
if (Input.GetMouButton(0))
{
cameraIsRotate = fal;
h2 = Input.GetAxis("Mou X") * rotationDamping;
Vector3 originalPosition = transform.position;
Quaternion originalRotation = ation;
transform.RotateAround(target.position, target.up, h2);    //决定因素position和rotation
float x = transform.eulerAngles.x;
做鬼脸的英语}
if (Input.GetMouButtonUp(0))
{
//offt = (transform.eulerAngles.y + 360 - oldRotationAngle)%360;
//Debug.Log(offt);
oldRotationAngle = transform.eulerAngles.y;
cameraIsRotate = true;
}
}
}
思路:
两种镜头旋转⽅式:
1.只旋转⼈物
2.只旋转背景。
需要带有拉近拉远镜头的功能。
旋转⼈物的时候就要把相机位置锁死,⽽旋转背景的时候,左右旋转要把相机的Y位置锁死,上下旋转的时候要把相机的X,Z位置分别锁死。
update⾥⾯要不断更新相机的位置,可以⽤offt(相机和⼈物的相对位置,向量差)。
⽽旋转⼈物的时候简单,offt不变。
旋转背景时,左右旋转,只修改offt的Y向量,(更新相机的Y位置,然后相减)。
上下旋转同理只修改offt的X,Z位置。
这样就实现了相机的跟随和镜头旋转。

本文发布于:2023-05-14 13:49:35,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/108202.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:旋转   跟随   镜头   位置   时候   点击   控制
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图