unity, 让主角头顶朝向等于地面法线(character align to surface normal)
计算过程如下:
1,通过由主角中心raycast一条竖直射线获得主角所在处地面法线,用作主角的newUp。
注:一定要从主角中心raycast,而不要从player.transform.position,因为如果player的模型原点在脚上,则player.transform.position可能就在地面以下了,向下raycast得不到与地面的交点了就。
2,根据主角forward和newUp计算newForward。
3,使用Quaternion.LookRotation (newForward, newUp)获得主角新的rotation。
结果如图:

代码:
//note, the code here suppose our character use a capsuleCollider and the floors' layerMask is "floor"..
//..if yours' is not, you should make some change.
RaycastHit hitInfo;
Vector3 capsuleColliderCenterInWorldSpace=GetComponent<CapsuleCollider> ().transform.TransformPoint (GetComponent<CapsuleCollider>().center);
bool isHit=Physics.Raycast (capsuleColliderCenterInWorldSpace,new Vector3(0f,-1f,0f),out hitInfo,100f,LayerMask.GetMask("floor"));
Vector3 forward=GetComponent<Rigidbody>().transform.forward;
Vector3 newUp;
if (isHit) {
newUp = hitInfo.normal;
} else {
newUp = Vector3.up;
}
//limit lean angle (if do not need to limit lean angle, remove the code block below)
{
const float maxDegreeFromWorldUp = 45.0f;//lean angle limited to 45 degree
//clamp newUp to let is not exceed 45 degree from WorldUp(Vector3.up)
newUp=Vector3.RotateTowards (Vector3.up, newUp, maxDegreeFromWorldUp / 180.0f * Mathf.PI, 0.0f);
}
Vector3 left = Vector3.Cross (forward,newUp);//note: unity use left-hand system, and Vector3.Cross obey left-hand rule.
Vector3 newForward = Vector3.Cross (newUp,left);
Quaternion oldRotation=GetComponent<Rigidbody>().transform.rotation;
Quaternion newRotation = Quaternion.LookRotation (newForward, newUp);
float kSoftness=0.1f;//if do not want softness, change the value to 1.0f
GetComponent<Rigidbody> ().MoveRotation (Quaternion.Lerp(oldRotation,newRotation,kSoftness));
unity, 让主角头顶朝向等于地面法线(character align to surface normal)的更多相关文章
- 【OpenGL】法线变换详解(Normal Transform)[转]
http://blog.csdn.net/xiajun07061225/article/details/7762711 在图形学中,同样的一个模型视图变换矩阵可以用来变换点.线.多边形以及其它几何体, ...
- Unity Shader入门教程(二)最基本的Diffuse和Normal样例
本教程参考了<猫都能学会的Unity3dShaderLab教程.CHM>, 1.请上网搜索并下载此文件. 2.随后再下载里面提到的素材: http://vdisk.weibo.com/s/ ...
- 【Unity Shaders】法线纹理(Normal Mapping)的实现细节
写在前面 写这篇的目的是为了总结我长期以来的混乱.虽然题目是"法线纹理的实现细节",但其实我想讲的是如何在shader中编程正确使用法线进行光照计算.这里面最让人头大的就是各种矩阵 ...
- 【Unity Shaders】Reflecting Your World —— Unity3D中的法线贴图和反射
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【Unity Shader】(四) ------ 纹理之法线纹理、单张纹理及遮罩纹理的实现
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...
- Unity 用户手册用户指南二维纹理 (Texture 2D)
http://www.58player.com/blog-2327-953.html 二维纹理 (Texture 2D) 纹理 (Textures) 使您的 网格 (Meshes).粒子 (Parti ...
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》第四步--地图循环&主角加入动作
说明:这里是借鉴:晓风残月前辈的博客,他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...
- 【Unity Shader】新书封面 — Low Polygon风格的渲染
写在前面 最近又开心又担心,因为我的书马上就要上市了,开心当然是因为等了这么久终于可以如愿了,担心是因为不少人对它的期待都很大,我第一次写书,能力也有限,不知道能不能让大家满意,让大家也都喜欢上它.不 ...
- unity surface shader 1
Unity ShaderLib : CGPROGRAM ENDCG之间是CG代码,之外的代码功能都由ShaderLib提供,CG中的一些方法比如tex2D(...)也是ShaderLib对CG进行 ...
随机推荐
- Lessons learned developing a practical large scale machine learning system
原文:http://googleresearch.blogspot.jp/2010/04/lessons-learned-developing-practical.html Lessons learn ...
- COM的一些基本概念
Windows lets you share code at the binary level using DLLs. After all, that's how Windows apps fun ...
- [Algorithm] Write your own Math.pow function in Javascript, using Recursive approach
/**@abstract * Write your own Math.pow(a int, b int) function * */ function pow (a, b) { let result ...
- [Backbone] Parse not formatted JSON code
The good Dr. recently had another team implement the server and they slightly messed up the format o ...
- PS中标尺工具在哪里
打开photoshop应用程序,然后在PS菜单中点击“视图——标尺”选项即可打开ps标尺栏. 单位设置: 按Ctrl+K快捷键会弹出“首选项”对话框,你选择“单位与标尺”项,在“单位”一栏里 ...
- 如何使用Flash抓抓狂抓取网页Flash
运行该软件,如果把鼠标移动到flash的部位浏览器左边顶部没有出现保存按钮,则定位到这个Flash,右击选择播放,暂停,反复一次即可看到. 把flash暂停再播放即可. 如果是QQ空间的漂亮的背景,不 ...
- Parse 使用- iOS 后台数据[转]
原文地址:http://blog.csdn.net/vipwangl/article/details/8846415 最近在学习Parse,但是Parse的中文教程比较少,看到这篇英文教程,把它翻译一 ...
- 微信小程序 - 自定义swiper(dot)指示点
点击下载示例:自定义swiper(dot)指示点
- PHP高级教程-异常处理(Exception)
PHP 异常处理 异常用于在指定的错误发生时改变脚本的正常流程. 异常是什么 PHP 5 提供了一种新的面向对象的错误处理方法. 异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程.这种情况 ...
- SVN mime-type 笔记
背景: 1.最近使用执行svn diff的时候发现有些文本文件无法显示: 2.浏览器会通过判断获取文件的 MIME 类型, 调用不同的客户端程序或使用不同的方式来执行.如果文件的 MIME 缺失或者有 ...