转一篇Unity的相机动画控制

最近真是忙,连研究细看的时间都没有了,原帖地址:https://alastaira.wordpress.com/2013/11/08/smooth-unity-camera-transitions-with-animation-curves/

先贴到这里

I’m creating a game in which I want to transition between two camera views – a top-down, overhead camera that gives a zoomed-out world view, and an over-the-shoulder style forward-facing camera to give a close-up view. Let’s say for the sake of discussion that these positions are defined relative to the player, as follows:

  • Overview: Position x/y/z: (0, 100, 0). Rotation x/y/z: (-90, 0, 0)  (60 units above, rotated straight down)
  • Close-up: Position x/y/z: (0, 0, –0.5). Rotation x/y/z: (0, 0, 0) (0.5 units behind, aligned with player direction)

Rather than simply cut between the two camera views, or present the top-down view in a second minimap camera alongside the main game view,  I want to use a single camera and transition smoothly between the two view locations. Initially, I tried to simply interpolate the position and rotation of the camera between the two locations (using lerp() for linear interpolation of the position vectors and slerp() for the rotation quaternions), based on a single zoom parameter:

transform.position = Vector3.Lerp(zoomstart.position, zoomend.position, zoom);

transform.rotation = Quaternion.Slerp(zoomstart.rotation, zoomend.rotation, zoom);

This gives:

However, this didn’t create the effect I wanted – I wanted to remain largely looking down as the camera zoomed in, and only pull up to match that player’s look vector at relatively close zoom levels, and also to ease the movement at both ends of the zoom.

My next thought was to ease in/out the camera motion using one of the functions described in this earlier post – perhaps a sigmoid curve, so that the rate of rotation was greatest at either extreme of the zoom spectrum. But I had trouble finding a function that exactly described the motion I wanted to create.

And then I discovered Unity’s AnimationCurves. I don’t know why they’re called animation curves, because they’re not specific to animations at all – they allow you to define custom curves based on an arbitrary number of control points (keys), and then evaluate the value of the function at any point along the curve.

To look up the value at any point along the curve use the Evaluate()  method. For example, in the curve above, the y value of the curve at x=0.5 is given by Curve.Evaluate(0.5) , which results in 0.692. Using this you can smoothly adjust any variable based on a single dimensional input.

Curves for Smooth Camera Movement

I have three camera transform parameters that change between my two camera locations – the offset position in y and z axes, and the rotation in the x axis. Since each animation curve can express only one dimension, I thought I might need to create three animation curves – one to describe each parameter that varies as the camera zooms in. However, on more thought I decided a better way: to have one curve to control the rotation around the x axis (i.e. adjusting the pitch of the camera), and then a second curve to set the distance which the camera should be offset back based on the forward direction of the camera at that point. Since the camera rotates about the x axis, translating back along its normal means that the second curve will govern both the height (y) and offset (z) in a single curve.

You can create animation curves in the inspector, but I initialised them in code as follos:

public class CameraScript : MonoBehaviour {

    // How camera pitch (i.e. rotation about x axis) should vary with zoom

    public AnimationCurve pitchCurve;

    // How far the camera should be placed back along the chosen pitch based on zoom

    public AnimationCurve distanceCurve;

    // Use this for initialization

    void Start () {

        // Create ‘S‘ shaped curve to adjust pitch

        // Varies from 0 (looking forward) at 0, to 90 (looking straight down) at 1

        pitchCurve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 90.0f);

       

        // Create exponential shaped curve to adjust distance

        // So zoom control will be more accurate at closer distances, and more coarse further away

        Keyframe[] ks = new Keyframe[2];

        // At zoom=0, offset by 0.5 units

        ks[0] = new Keyframe(0, 0.5f);

        ks[0].outTangent = 0;

        // At zoom=1, offset by 60 units

        ks[1] = new Keyframe(1, 60);

        ks[1].inTangent = 90;

        distanceCurve = new AnimationCurve(ks);

    }

}

This generates the following curves:

Pitch

Distance

I then evaluate the appropriate point from each curve based on the camera zoom level, as follows:

// Calculate the appropriate pitch (x rotation) for the camera based on current zoom      

float targetRotX = pitchCurve.Evaluate(zoom);

// The desired yaw (y rotation) is to match that of the target object

float targetRotY = target.rotation.eulerAngles.y;

// Create target rotation as quaternion

// Set z to 0 as we don‘t want to roll the camera

Quaternion targetRot = Quaternion.Euler(targetRotX, targetRotY, 0.0f);

       

// Calculate in world-aligned axis, how far back we want the camera to be based on current zoom

Vector3 offset = Vector3.forward * distanceCurve.Evaluate(zoom);

// Then subtract this offset based on the current camera rotation

Vector3 targetPos = target.position - targetRot * offset;

Finally, rather than setting the camera position/rotation directly, I lerp/slerp between the current transform.position/rotation towards the target position/rotation to smooth the movement and get the result as follows, which I’m pretty happy with (scene view on the left, resulting camera view on the right):

时间: 2024-10-29 19:10:45

转一篇Unity的相机动画控制的相关文章

关于Unity中Mecanim动画的动画状态代码控制与代码生成动画控制器

对于多量的.复杂的.有规律的控制器使用代码生成 动画状态代码控制 1:每个动画状态,比如进入状态,离开状态, 等都有可能需要代码来参与和处理,比如,进入这个动画单元后做哪些事情,来开这个动画单元后做哪些事情,为了解决这个问题,unity允许每个动画单元来绑定一个脚本代码,这个脚本代码必须继承于StateMachineBehaviour;2: 可以在动画状态的Add Behaviour上添加挂载一个脚本到动画状态;3: StateMachineBehaviour主要接口: (1)OnStateEn

unity 对Animator动画系统的研究

unity的新动画系统叫Mecanim,使用Animator来取代旧系统Animation,按Unity文档的惯例:知识点主要分2部分:unity manual和unity script,读者可以边看文章边查阅文档,最好能动手测试. 文章的开始之前,先讲几个基本的知识的: 1.创建动画的一个基本步骤是设置一个unity3d可理解的简化后的骨骼到骨架中实际骨骼的映射:在Mecanim的术语中,这个映射称为Avatar,即avatar是骨骼到骨架的映射. (图片来自网) Avatar主要用于类人骨骼

Unity MegaFiers 顶点动画

使用 MegaFiers 插件,可以使得Unity支持顶点动画的播放. 官方视频教程如下: 在这里简单测试使用下,环境如下: Blender 2.72 Unity 4.5.4 MegaFiers 2.74 创建物体和动画 首先打开Blender,默认会含有一个立方体,下面对这个立方体制作一个简单动画,让其在1~100帧做旋转,101~250帧做缩放,制作方法可参考教程[牛刀小试简单动画]. 确认立方体处于桔黄色外框包围的"被选中"状态: 用鼠标把绿色的当前帧指示线拖到第1帧: 在&qu

unity shader序列帧动画代码,顺便吐槽一下unity shader系统

http://www.cnblogs.com/hellohuan/archive/2014/01/10/3512784.html 一.看到UNITY论坛里有些人求unity shader序列帧动画,写shader我擅长啊,就顺势写了个CG的shader.代码很简单,就是变换UV采样序列帧贴图,美术配置行数列数以及变换速度. Shader "HELLOHUAN/Hello_Sequence" { Properties { _Color ("Main Color", C

深入学习jQuery动画控制

× 目录 [1]动画状态 [2]停止动画 [3]动画延迟[4]全局控制 前面的话 jQuery动画可以使用fade.hide.slide等方法实现基本动画效果,可以使用animate实现自定义动画,甚至可以使用queue实现动画队列.但是,却缺少了对动画控制的介绍.动画产生后,描述动画状态.进行动画延迟.操作动画暂停等都是很重要的功能.本文将详细介绍jQuery动画控制 动画状态 当用户快速在某个元素多次执行动画时,会造成动画累积的现象.这时,就需要引入动画状态这个概念.判断元素是否处于动画状态

Python学习(二):入门篇:python中流程控制与函数编写

python中流程控制与函数编写 Last Eidt 2014/5/2 转载请注明出处http://blog.csdn.net/jxlijunhao 一,流程控制 1)布尔逻辑 Python中利用True来表示逻辑真,False来逻辑假 not :非 and:与 or   :或 ==  :逻辑等 >>> False==True False >>> False==False True >>> not False True >>> Fal

iOS开发Swift篇—(六)流程控制

iOS开发Swift篇—(六)流程控制 一.swift中的流程控制 Swift支持的流程结构如下: 循环结构:for.for-in.while.do-while 选择结构:if.switch 注意:这些语句后面一定要跟上大括号{},在C语言中不是必须的 说明:跟C语言对比,用法基本一样的有:for.while.do-while.if 因此,只需要关注for-in和switch即可 二.for-in结构 简单使用: for-in和范围运算符 for i in 1...3 { println(i)

在Unity中实现动画的正反播放代码

using UnityEngine; using System.Collections; public class AnimationAntiSowing : MonoBehaviour { public static AnimationAntiSowing _initialise; void Awake() { _initialise = this; } /// <summary> /// 动画进行正反播放 /// </summary> /// <param name=&q

unity中camera摄像头控制详解

目录 1. 缘起 2. 开发 2.1. 建立项目 2.2. 旋转 2.2.1. 四元数 2.3. 移动 2.3.1. 向量操作 2.4. 镜头拉伸 2.5. 复位 2.6. 优化 1 缘起 我们的产品是使用unity开发水利BIM(水利建筑信息模型),项目中需要控制摄像 头对模型进行360度查看,请注意所有操作都是移动摄像头,不是移动模型.摄 像头能进行移动.旋转.改变焦距操作,类似于SketchUp的控制操作: 摄像头移动时,根据当前旋转方向(Rotation)进行移动 摄像头距离模型越远,摄