[UE4]-Pitch轴旋转

不用使用Set***Rotation来设置旋转,会出现转不过去的情况,这种情况是引擎为了避免出现万向锁,将Pitch(Y轴)限定在90~-90之间,更多解释可以搜搜万向锁。

但是我们就是要一个轴进行旋转,并且使他超过90或-90,我们使用Add***Rotation来解决这个问题,使用Add函数即可进行任意旋转。

题外话:

为什么不用四元数或旋转矩阵呢?答案是没用啊(或许我没找对方法)

### 实现鼠标控制物体绕指定旋转 在Unreal Engine 4中,要实现通过鼠标控制物体绕特定旋转的功能,可以利用`Axis Mapping`机制[^3]。这涉及到捕捉鼠标的移动并将其转换成旋转角度应用到目标对象上。 对于此功能的具体实施,主要分为几个方面考虑: - **输入处理**:定义新的映射用于接收鼠标的X和Y方向上的位移变化。 - **逻辑计算**:基于接收到的鼠标位置改变量,计算出相应的旋转增量,并更新被控物的位置属性。 下面是一份简单的蓝图脚本以及对应C++代码片段来说明这个过程。 #### 蓝图解决方案 1. 打开项目的Input Settings,在Bindings标签页里新增两个Float类型的Axis Mappings分别命名为“MouseX” 和 “MouseY”,它们用来表示水平与垂直方向上的鼠标偏移值。 2. 创建一个新的Actor Blueprint作为可交互的对象实例化于场景之中。 3. 在该Blueprint内部添加Event Graph节点 `Add Local Rotation` 来响应上述自定义的 Axis Events (`MouseX`, `MouseY`) ,从而调整自身的朝向。 ```blueprint // Event Tick -> Get Delta Seconds (float) * Mouse Sensitivity (float) // Multiply by -1 if needed to invert axis direction. // Pass result into AddLocalRotation as Pitch/Yaw/Roll components accordingly. ``` #### C++ 解决方案 如果倾向于采用编程方式,则可以在角色类或其他合适的组件内重写Tick函数监听来自用户的输入指令,并据此修改Transform矩阵中的Rotator成员变量。 ```cpp void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); // Bind the mouse movement input to our custom function PlayerInputComponent->BindAxis("Turn", this, &AMyCharacter::Turn); } void AMyCharacter::Turn(float Value) { if (!bCanRotate || FMath::IsNearlyZero(Value)) return; FRotator NewRotation = GetActorRotation(); // Apply rotation based on mouse delta value and sensitivity setting float YawDelta = Value * BaseTurnRate * GetWorld()->GetDeltaSeconds(); NewRotation.Yaw += YawDelta; SetActorRotation(NewRotation); } ``` 以上就是关于如何在Unreal Engine 4 中使用鼠标控制物体沿某一固定线做圆周运动的方法概述及其具体实践指南[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值