RotateAround和Quaternion.AngleAxis
private void Update()
{
//camera.transform.LookAt(target.transform);
//camera.transform.RotateAround(target.transform.position, new Vector3(1, 0, 0), speed * Time.deltaTime); //绕世界的x旋转
//camera.transform.RotateAround(target.transform.position, target.transform.right, speed * Time.deltaTime); //目标x旋转
//camera.transform.RotateAround(target.transform.position, camera.transform.right, speed * Time.deltaTime); //绕自身x旋转
//camera.transform.RotateAround(target.transform.position, camera.transform.forward, speed * Time.deltaTime); //绕自身朝向旋转
camera.transform.RotateAround(target.transform.position, new Vector3(-camera.transform.forward.z, 0, camera.transform.forward.x), -speed * Time.deltaTime); //绕自身朝向的垂直向量x旋转,这是我们想要的
}
所以归结要想沿着注视物体的x轴旋转,要先lookat,然后再取foward的垂直向量旋转。在Start中lookat一次即可。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public Transform target;
public Transform camera;
void Start()
{
newVec = camera.position - target.position;
camera.LookAt(target);
}
public float speed = 10;
public float scrollSpeed = 10;
public Vector3 newVec;
public float addX;
public float addY;
void Update()
{
camera.LookAt(target);
if (Input.GetKey(KeyCode.A))
{
newVec = camera.position - target.position;
addX += Time.deltaTime * speed;
Vector3 vv = Quaternion.AngleAxis(Time.deltaTime * speed, new Vector3(-camera.forward.z, 0, camera.forward.x)) * newVec;
camera.transform.position = target.position + vv;
}
if (Input.GetKey(KeyCode.B))
{
newVec = camera.position - target.position;
addX -= Time.deltaTime * speed;
Vector3 vv = Quaternion.AngleAxis(-Time.deltaTime * speed, new Vector3(-camera.forward.z, 0, camera.forward.x)) * newVec;
camera.transform.position = target.position + vv;
}
if (Input.GetKey(KeyCode.C))
{
newVec = camera.position - target.position;
addY += Time.deltaTime * speed;
Vector3 vv = Quaternion.AngleAxis(Time.deltaTime * speed, target.up) * newVec;
camera.transform.position = target.position + vv;
}
if (Input.GetKey(KeyCode.D))
{
newVec = camera.position - target.position;
addY -= Time.deltaTime * speed;
Vector3 vv = Quaternion.AngleAxis(-Time.deltaTime * speed, target.up) * newVec;
camera.transform.position = target.position + vv;
}
if(Input.GetKey(KeyCode.E))
{
newVec = camera.position - target.position;
float magnitued = newVec.magnitude;
magnitued -= Time.deltaTime * scrollSpeed;
newVec = newVec.normalized * magnitued;
camera.transform.position = target.position + newVec;
}
if (Input.GetKey(KeyCode.F))
{
newVec = camera.position - target.position;
float magnitued = newVec.magnitude;
magnitued += Time.deltaTime * scrollSpeed;
newVec = newVec.normalized * magnitued;
camera.transform.position = target.position + newVec;
}
Debug.DrawLine(target.position, camera.position);
}
}
读camera的整理。
BasicCameraController类
他的基类为CameraController
首先是成员Camera。
然后是Awake方法。
属性变量
public bool IsInitialized {get; private set;}
protected void Awake()
{
IsInitialized = false;
if(Camera!=null)
{
CameraTransform = new VirtualTransform();
}
else
{
throw new …
}
if(!ComponentsAdded
{
_components = new Dictionary<Type, CameraComponent>(;
AddCameraComponents(;
ComponentsAdded = true;
}
InitializeComponents(;
}
protected void InitializeComponents(
{
forearch(CameraComponent component in _components.Values
{
component.Initialize(this;
}
IsInitialized=true;
}
public void AddCameraComponent(CameraComponent component)
{
…
…
if(_components.ContainsKey(type))
{
throw new xxxx
}
_components.Add(type,component);
}
什么是VirtualTransform
虚拟的Transform
BasicCameraController的成员方法
protected override void AddCameraComponents(
{
AddCameraComponent(Rotation);
AddCameraComponent(Zoom);
AddCameraComponent(Target);
AddCameraComponent(ViewCollision);
AddCameraComponent(Input);
AddCameraComponent(EasyUnityInput);
AddCameraComponent(Cursor);
AddCameraComponent(Headbob);
AddCameraComponent(ScreenShake);
}
RotationComponent
public void Rotate(float horizontalDegrees, float verticalDegress)
{
RotateHorizontally(horizontalDegrees);
RotateVertically(verticalDegrees);
}
public void RotateHorizontally(float degrees)
{
if…
return;
if (Limits.EnableHorizontalLimits)
{
degrees = GetEnforcedHorizontalDegrees(degrees);
}
CameraTransform.Rotate(_orientationTransform,up, degrees, Space.World); //朝向物体的,水平旋转degrees。
TrackHorizontalLimitsRotation(degrees);
}
private void TrackHorizontalLimitsRotation(float degrees)
{
unchecked
{
_horizontalRotationLimitsTotal += degrees;
}
}
限制本次的上限
public float GetEnforcedHorizontalDegrees(float degrees)
{
// rotating right
if (degrees < 0)
{
//_horizontalRotationLimitsTotal累加值,比如为-85,degrees为-6,Limits.HorizontalRight=90
//-85-6=-91,小于-90,所以受限制之后,返回的是-5。
if (_horizontalRotationLimitsTotal + degrees < Limits.HorizontalRight)
{
return Limits.HorizontalRight - _horizontalRotationLimitsTotal;
}
}
// rotating left
else if (degrees >= 0)
{
if (_horizontalRotationLimitsTotal + degrees > Limits.HorizontalLeft)
{
return Limits.HorizontalLeft - _horizontalRotationLimitsTotal;
}
}
}
垂直旋转
public void RotateVertically(float degrees)
{
if (!Enabled || _isExternalDisabled)
{
return;
}
_smartFollowActivated = _smartFollowRotationRetain;
if (Limits.EnableVerticalLimits)
{
degrees = GetEnforcedVerticalDegrees(degrees);
}
CameraTransform.Rotate(CameraTransform.Right, degrees, Space.World);
if (VerticalDegreesEvent.Enabled)
{
TrackVerticalEventRotation(degrees);
}
TrackVerticalLimitsRotation(degrees);
}
BasicCameraComponent
void Start()
{
…
_previousDistance = DesiredDistance;
UpdateCamera();
}
更新摄像机
FixedUpdate中执行。
public override void UpdateCamera()
{
EasyUnityInput.AppendInput();
}
public void AppendInput()
{
}
推荐阅读
-
RotateAround和Quaternion.AngleAxis
-
vue 和animate.css动画使用
-
css实现 display: none和display: block的切换动画
-
防抖和节流解决方案
-
Vue动画之transition和keyframes关键帧动画
-
Android版&Kotlin版RxJava2+Retrofit2+OkHttp3的基础、封装和项目中的使用
-
http认证原理和https 博客分类: https https
-
div+css 完全兼容ie6 ie7 IE8 IE9 和firefox方法(转载) 博客分类: css\html
-
【android应用】android应用开发中,package相关信息的判断和获取--系统权限、版本号、包名等
-
Linux和Windows之间文件传输(Linux下rz,sz与ssh的配合使用) 博客分类: Linux linuxwindowsrzsz文件传输