当前位置:网站首页>Unity限制旋转角度
Unity限制旋转角度
2022-04-21 06:27:00 【菜菜码农柠檬哥】
游戏制作时,有时会有限制某些物体旋转角度的需求,可以使用以下代码进行限制,此方法为使用了Mathf.Clamp来返回最大值和最小值之间的值,使填入的值不超出解限,然后不断赋值物体给新的旋转坐标,下面为限制Y轴的示例。
LimitViewY(-0.4f,0.35f);
public void LimitViewY(float minView,float MaxView)
{
float rotationY = Mathf.Clamp(Player.transform.rotation.y, minView, MaxView);
Quaternion a = new Quaternion(Player.transform.rotation.x, rotationY, Player.transform.rotation.z, Player.transform.rotation.w);
Player.transform.rotation = a;
}
稍加修改可以限制其他轴,以Z轴为例:
LimitViewZ(-0.2f,0.2f);
public void LimitViewZ(float minView,float MaxView)
{
float rotationZ = Mathf.Clamp(Player.transform.rotation.z, minView, MaxView);
Quaternion a = new Quaternion( Player.transform.rotation.x, Player.transform.rotation.y , rotationZ, Player.transform.rotation.w);
Player.transform.rotation = a;
}
PS:在EasyTouch插件中限制摇杆旋转角度,在ETCAxis脚本当中,找到DoDirectAction方法,在其中限制即可:
public void DoDirectAction()
{
if (directTransform){
Vector3 localAxis = GetInfluencedAxis();
Player = GameObject.FindWithTag("Player");
switch (directAction){
case ETCAxis.DirectAction.Rotate:
LimitViewY(-0.4f,0.35f);
directTransform.Rotate( localAxis * axisSpeedValue, Space.World);
break;
case ETCAxis.DirectAction.RotateLocal:
LimitViewZ(-0.2f,0.2f);
directTransform.Rotate( -localAxis * axisSpeedValue,Space.Self);//改变轴向
break;
}
版权声明
本文为[菜菜码农柠檬哥]所创,转载请带上原文链接,感谢
https://blog.csdn.net/y1139735983/article/details/120722478
边栏推荐
猜你喜欢
随机推荐
Solve the problem that the replacement traverses the circular call, resulting in the subsequent replacement replacing the data of the previous replacement
PowerShell - because running scripts is prohibited on this system
【LeetCode 350】两个数组的交集 II
图形学基础|抗锯齿(Anti-Aliasing)
Could not initialize cudnn ,please check cudnn installation.
POJ - 2955 Brackets 区间dp
网络攻防安全学习平台-上传关3
虚幻引擎之Sequence特效和音频绑定触发
Database splitting under microservice architecture
动画—Keyframes介绍
Integers Have Friends 区间gcd + 双指针
如何利用JMeter和Jprofiler对软件进行性能测试和优化定位
2022牛客寒假补题记录 2
PowerShell - 因为在此系统上禁止运行脚本
一次从 EXSI 移植 vmfstools 失败的过程记录
C语言版:链栈的建立和基本操作
Fundamentals of graphics | cartoon shadow map based on SDF
pg 数据库不能使用 zh_CN.UTF-8:initdb: error: locale “zh_CN.UTF-8“ requires unsupported encoding “GBK“
Sakura Substring思维
MMIO 与 PMIO 技术








