UE4中关于如何拾取物体进行移动的方法
近期课题要求制作一个用户随意拾取物体并移动到任意地方的功能,在我写好抓取和移动函数之后,发现无法实现预想中的目标。
这个问题困扰了三天(我承认我在召唤师峡谷走了一天,在开拓者和Lakers的对战中走了两天),今晚终于得到了解决。
先放张美女养养眼,释放一下徘徊在代码中的眼睛们
一、设置抓取和释放功能。
1.Create A Pawn
(1)Create a BP which is based on “Default Pawn”
(2)Throw this pawn to the world.
(3)Set the pawn possession:player 0,placed in world or spawned(让pawn创建后被第一个玩家操控,并自动被AI控制器占有)
2.Improve
(1)Set max speed with a proper value:Movement Component detail-Floating Pawn Movement -max speed
(2)Add a “hand”:add Static Mesh-choose a sphere-set its scale,position and collision(of course no collision).
(3)Set pawn rotation:input “rotation” in the detail plate,set :use controller rotation pitch/yaw/roll
3.Realize the function that grab the mesh when click mouse and release it when release the mouse:
(1)Set event:
①Find “left mouse button” and give it 2 custom events
②Rename the sphere “Hand”
③Get the “hand”s location-add a node which is “sphere Overlap Actors”
④Set the para of this node:
1)Sphere radius:10
2)Object type:make array-choose world dynamic
3)Actor class filter:static mesh actors
⑤Give a branch:if we get some mesh(that means the return value is true),add this “out actors” as a variable named “hand actor”.
⑥Attach it
1)Give the “hand actor” a node which name is “Attach To Component”
2)Set its parent “Hand”
3)Set the location/rotation/scale rule :keep world
⑦Set the “release” event:give it a node named “Detach From Actor”,the target is the “Hand Actor”,and rules are as above.
⑧Snap actor:this is the most important step
注:此功能我是在某站跟一个老外学的,没有字幕,边听边记笔记没有翻译成汉语。老外还用了插件,非常的牛北,可惜插件我暂时找不到。
二、设置想释放的物体
1.创建一个蓝图类,此类基于StaticMeshActor,为什么加粗了呢?因为我一直是基于简单的Actor,所以一直无法拾取。
2.设置相关属性
(1)设置static mesh以及material
(2)开启模拟物理(不开启是无法抓取的)
(2)查找抓取物体的类型,并在Grab函数中修改预设值。
可以看到这个物体的Type是Physics Body,那么
把这个值改成相同的类型。
三、效果
可见,这个设备已经被我用鼠标抓住,并移动到空中了,释放鼠标,则此物体停留在鼠标处。