[unity3d]第三人称视角相关脚本
脚本 相关 Unity3D 视角
2023-09-27 14:29:23 时间
// the height we want the camera to be above the target
var height = 3.0; //相机距离物体的高度
var angularSmoothLag = 0.3;
var angularMaxSpeed = 15.0;
var heightSmoothLag = 0.3;
var snapSmoothLag = 0.2;
var snapMaxSpeed = 720.0;
var clampHeadPositionScreenSpace = 0.75;
var lockCameraTimeout = 0.2;
private var headOffset = Vector3.zero;
private var centerOffset = Vector3.zero;
private var heightVelocity = 0.0;
private var angleVelocity = 0.0;
private var snap = false;
private var controller : ThirdPersonController;
private var targetHeight = 100000.0;
function Awake ()
if(!cameraTransform Camera.main)
cameraTransform = Camera.main.transform; //获取主相机
if(!cameraTransform) {
Debug.Log("Please assign a camera to the ThirdPersonCamera script.");
enabled = false;
var characterController : CharacterController = _target.collider; centerOffset = characterController.bounds.center - _target.position; headOffset = centerOffset; headOffset.y = characterController.bounds.max.y - _target.position.y; else Debug.Log("Please assign a target to the camera that has a ThirdPersonController script attached.");
var targetCenter = _target.position + centerOffset; var targetHead = _target.position + headOffset; // DebugDrawStuff(); // Calculate the current target rotation angles var originalTargetAngle = _target.eulerAngles.y; var currentAngle = cameraTransform.eulerAngles.y; // Adjust real target angle when camera is locked var targetAngle = originalTargetAngle; // When pressing Fire2 (alt) the camera will snap to the target direction real quick. // It will stop snapping when it reaches the target if (Input.GetButton("Fire2")) snap = true; if (snap) // We are close to the target, so we can stop snapping now! if (AngleDistance (currentAngle, originalTargetAngle) 3.0) snap = false; currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, snapSmoothLag, snapMaxSpeed); // Normal camera motion else if (controller.GetLockCameraTimer () lockCameraTimeout) targetAngle = currentAngle; // Lock the camera when moving backwards! // * It is really confusing to do 180 degree spins when turning around. if (AngleDistance (currentAngle, targetAngle) 160 controller.IsMovingBackwards ()) targetAngle += 180; currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, angularSmoothLag, angularMaxSpeed);
// Wed be moving the camera upwards, do that only if its really high var newTargetHeight = targetCenter.y + height; if (newTargetHeight targetHeight || newTargetHeight - targetHeight 5) targetHeight = targetCenter.y + height; // When walking always update the target height else targetHeight = targetCenter.y + height; // Damp the height var currentHeight = cameraTransform.position.y; currentHeight = Mathf.SmoothDamp (currentHeight, targetHeight, heightVelocity, heightSmoothLag); // Convert the angle into a rotation, by which we then reposition the camera var currentRotation = Quaternion.Euler (0, currentAngle, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target cameraTransform.position = targetCenter; cameraTransform.position += currentRotation * Vector3.back * distance; // Set the height of the camera cameraTransform.position.y = currentHeight; // Always look at the target SetUpRotation(targetCenter, targetHead); function LateUpdate () { Apply (transform, Vector3.zero);
// Now its getting hairy. The devil is in the details here, the big issue is jumping of course. // * When jumping up and down we dont want to center the guy in screen space. // This is important to give a feel for how high you jump and avoiding large camera movements. // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth. // So here is what we will do: // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis // 2. When grounded we make him be centered // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold // 4. When landing we smoothly interpolate towards centering him on screen var cameraPos = cameraTransform.position; var offsetToCenter = centerPos - cameraPos; // Generate base rotation only around y-axis var yRotation = Quaternion.LookRotation(Vector3(offsetToCenter.x, 0, offsetToCenter.z)); var relativeOffset = Vector3.forward * distance + Vector3.down * height; cameraTransform.rotation = yRotation * Quaternion.LookRotation(relativeOffset); // Calculate the projected center position and top position in world space var centerRay = cameraTransform.camera.ViewportPointToRay(Vector3(.5, 0.5, 1)); var topRay = cameraTransform.camera.ViewportPointToRay(Vector3(.5, clampHeadPositionScreenSpace, 1)); var centerRayPos = centerRay.GetPoint(distance); var topRayPos = topRay.GetPoint(distance); var centerToTopAngle = Vector3.Angle(centerRay.direction, topRay.direction); var heightToAngle = centerToTopAngle / (centerRayPos.y - topRayPos.y); var extraLookAngle = heightToAngle * (centerRayPos.y - centerPos.y); if (extraLookAngle centerToTopAngle) extraLookAngle = 0; else extraLookAngle = extraLookAngle - centerToTopAngle; cameraTransform.rotation *= Quaternion.Euler(-extraLookAngle, 0, 0); function GetCenterOffset () return centerOffset;
封装库/工具库中重要概念之动画 前端开发中,动画是一个非常重要的技术特性。它可以提升用户体验,增加页面交互性,并且让网站看起来更加生动活泼。然而,在实现复杂动画时,手写代码往往会变得繁琐且容易出错。因此,前端工具库和封装库的出现为我们提供了便利。在本文中,我们将探讨前端中的封装库和工具库以及它们在实现动画效果方面的作用。
unity程序使用云渲染技术是否要修改? unity引擎开发的程序使用了点量云渲染系统后,为啥不用对程序修改就可以实现手机和平板中直接使用呢?这是因为云渲染系统,对于操控方式是键盘或者鼠标的Unity或者UE4程序,会在手机中模拟出鼠摇杆和软键盘,这样就可以像在电脑上一样完成交互了。当然如果手机或者平板能连蓝牙,也可以外接鼠标完成交互。**所以使用实时云渲染技术是无需对现有程序做修改的,程序在Windows使用是如何建模的,还是按照原来的方式建模即可
【Unity3D 灵巧小知识点】☀️ | Unity脚本生命周期 Unity小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 。 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。 Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏!
优化Unity游戏项目的脚本(上) 本文将由捷克独立游戏工作室Lonely Vertex的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法,并提供改进Unity游戏性能的最佳实践。 在开发游戏时,我们遇到了游戏过程中偶尔出现延迟的问题。
优化Unity游戏项目的脚本(下) 金秋9月,我们祝所有的老师们:教师节快乐 ! 今天,我们继续分享来自捷克的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法。 在优化Unity游戏项目的脚本(上)中,我们介绍了如何查找C#脚本中的问题,以及垃圾回收的处理。
unity 如何脚本间交互 如何脚本间交互:方法1: 通过在编辑器里面拖动,来持有这个对象去调用对应的函数,这个方法比较简单。 在编辑器中新建2个脚本。 我们写一个a脚本 public class Ascript : MonoBehaviour { // Use this for initialization void St...
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
var characterController : CharacterController = _target.collider; centerOffset = characterController.bounds.center - _target.position; headOffset = centerOffset; headOffset.y = characterController.bounds.max.y - _target.position.y; else Debug.Log("Please assign a target to the camera that has a ThirdPersonController script attached.");
var targetCenter = _target.position + centerOffset; var targetHead = _target.position + headOffset; // DebugDrawStuff(); // Calculate the current target rotation angles var originalTargetAngle = _target.eulerAngles.y; var currentAngle = cameraTransform.eulerAngles.y; // Adjust real target angle when camera is locked var targetAngle = originalTargetAngle; // When pressing Fire2 (alt) the camera will snap to the target direction real quick. // It will stop snapping when it reaches the target if (Input.GetButton("Fire2")) snap = true; if (snap) // We are close to the target, so we can stop snapping now! if (AngleDistance (currentAngle, originalTargetAngle) 3.0) snap = false; currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, snapSmoothLag, snapMaxSpeed); // Normal camera motion else if (controller.GetLockCameraTimer () lockCameraTimeout) targetAngle = currentAngle; // Lock the camera when moving backwards! // * It is really confusing to do 180 degree spins when turning around. if (AngleDistance (currentAngle, targetAngle) 160 controller.IsMovingBackwards ()) targetAngle += 180; currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, angleVelocity, angularSmoothLag, angularMaxSpeed);
// Wed be moving the camera upwards, do that only if its really high var newTargetHeight = targetCenter.y + height; if (newTargetHeight targetHeight || newTargetHeight - targetHeight 5) targetHeight = targetCenter.y + height; // When walking always update the target height else targetHeight = targetCenter.y + height; // Damp the height var currentHeight = cameraTransform.position.y; currentHeight = Mathf.SmoothDamp (currentHeight, targetHeight, heightVelocity, heightSmoothLag); // Convert the angle into a rotation, by which we then reposition the camera var currentRotation = Quaternion.Euler (0, currentAngle, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target cameraTransform.position = targetCenter; cameraTransform.position += currentRotation * Vector3.back * distance; // Set the height of the camera cameraTransform.position.y = currentHeight; // Always look at the target SetUpRotation(targetCenter, targetHead); function LateUpdate () { Apply (transform, Vector3.zero);
// Now its getting hairy. The devil is in the details here, the big issue is jumping of course. // * When jumping up and down we dont want to center the guy in screen space. // This is important to give a feel for how high you jump and avoiding large camera movements. // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth. // So here is what we will do: // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis // 2. When grounded we make him be centered // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold // 4. When landing we smoothly interpolate towards centering him on screen var cameraPos = cameraTransform.position; var offsetToCenter = centerPos - cameraPos; // Generate base rotation only around y-axis var yRotation = Quaternion.LookRotation(Vector3(offsetToCenter.x, 0, offsetToCenter.z)); var relativeOffset = Vector3.forward * distance + Vector3.down * height; cameraTransform.rotation = yRotation * Quaternion.LookRotation(relativeOffset); // Calculate the projected center position and top position in world space var centerRay = cameraTransform.camera.ViewportPointToRay(Vector3(.5, 0.5, 1)); var topRay = cameraTransform.camera.ViewportPointToRay(Vector3(.5, clampHeadPositionScreenSpace, 1)); var centerRayPos = centerRay.GetPoint(distance); var topRayPos = topRay.GetPoint(distance); var centerToTopAngle = Vector3.Angle(centerRay.direction, topRay.direction); var heightToAngle = centerToTopAngle / (centerRayPos.y - topRayPos.y); var extraLookAngle = heightToAngle * (centerRayPos.y - centerPos.y); if (extraLookAngle centerToTopAngle) extraLookAngle = 0; else extraLookAngle = extraLookAngle - centerToTopAngle; cameraTransform.rotation *= Quaternion.Euler(-extraLookAngle, 0, 0); function GetCenterOffset () return centerOffset;
封装库/工具库中重要概念之动画 前端开发中,动画是一个非常重要的技术特性。它可以提升用户体验,增加页面交互性,并且让网站看起来更加生动活泼。然而,在实现复杂动画时,手写代码往往会变得繁琐且容易出错。因此,前端工具库和封装库的出现为我们提供了便利。在本文中,我们将探讨前端中的封装库和工具库以及它们在实现动画效果方面的作用。
unity程序使用云渲染技术是否要修改? unity引擎开发的程序使用了点量云渲染系统后,为啥不用对程序修改就可以实现手机和平板中直接使用呢?这是因为云渲染系统,对于操控方式是键盘或者鼠标的Unity或者UE4程序,会在手机中模拟出鼠摇杆和软键盘,这样就可以像在电脑上一样完成交互了。当然如果手机或者平板能连蓝牙,也可以外接鼠标完成交互。**所以使用实时云渲染技术是无需对现有程序做修改的,程序在Windows使用是如何建模的,还是按照原来的方式建模即可
【Unity3D 灵巧小知识点】☀️ | Unity脚本生命周期 Unity小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 。 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。 Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏!
优化Unity游戏项目的脚本(上) 本文将由捷克独立游戏工作室Lonely Vertex的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法,并提供改进Unity游戏性能的最佳实践。 在开发游戏时,我们遇到了游戏过程中偶尔出现延迟的问题。
优化Unity游戏项目的脚本(下) 金秋9月,我们祝所有的老师们:教师节快乐 ! 今天,我们继续分享来自捷克的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法。 在优化Unity游戏项目的脚本(上)中,我们介绍了如何查找C#脚本中的问题,以及垃圾回收的处理。
unity 如何脚本间交互 如何脚本间交互:方法1: 通过在编辑器里面拖动,来持有这个对象去调用对应的函数,这个方法比较简单。 在编辑器中新建2个脚本。 我们写一个a脚本 public class Ascript : MonoBehaviour { // Use this for initialization void St...
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
相关文章
- Hadoop:群起脚本【xcall.sh】【一个命令启动/停止所有节点的Hadoop(NameNode,DataNode)、Yarn节点(ResourceManager, NodeManager)】
- 大数据-离线数仓-工作流调度-工具:DolphinScheduler【可视化DAG工作流任务调度平台】【全能、轻量级、功能强大、美观、不需要自己写flow脚本、直接在web页面操作、国产、中文文档】
- ant执行sql脚本
- [转贴]使用dbstart 和dbshut 脚本来自动化启动和关闭数据库
- mysql shell 脚本备份
- mysql 批处理命令执行多个sql脚本
- windows上自动设置java环境变量的脚本
- 玩转Bash脚本:选择结构之if
- 调用其他类函数,且其他类函数使用自己脚本的成员变量的方法
- 测试用例脚本 测试用例之间变量可以相互用的方法 实例
- Redis两种持久化方式原理和单机多实例持久化脚本
- 防止sql注入和跨站脚本攻击,跨站请求伪造以及一句话木马的学习记录
- 自动重启挂掉的python脚本
- c++多线程调用python脚本
- Linux Ubuntu 20.04 —添加开机启动(服务/脚本)
- shell 脚本练习 | 「题霸」面试必考真题【shell篇】题解
- 8、在Shell脚本中使用函数
- shell脚本中使用getops