[开发者心得] 如何更改NPC的朝向

[复制链接]
1393 |4
感情偏食BZ8 发表于 2023-8-6 10:26:54 | 显示全部楼层 |阅读模式
本帖最后由 感情偏食BZ8 于 2023-8-6 10:26 编辑

一.一分钟上手(简单使用)

更改NPC的朝向实际上非常简单只需要两行行代码

//通过在场景中的NPC的name获取NPC
let NPC = Gameplay.GameObject.getGameObjectByName("NPC") as Gameplay.NPC;
//更改想要的旋转即可
NPC.worldRotation = new Type.Rotation(0, 0, 100);


是不是非常简单,下面我们可以面对一些稍微复杂的情况做一些尝试。


二.朝向更改的进阶利用
      1.朝向指定物体的位置
      


        //首先还是获得NPC和朝向物体
        let NPC = Gameplay.GameObject.getGameObjectByName("NPC") as Gameplay.NPC;
        let cube = Gameplay.GameObject.getGameObjectByName("cube");
        //通过两者位置的获取物体相对玩家的向量
        let location = cube.worldLocation.clone().subtract(NPC.worldLocation.clone());
        //通常只需要更改Z轴
        let rotation = NPC.worldRotation.clone();
        //把两者的向量转化成方向
        rotation.z = location.toRotation().z;
        NPC.worldRotation = rotation;

OK运行这段代码以后我们就会发现场景中名字叫“NPC”的人物会朝向名字叫cube的方块具体看下图

代码运行前

image.png
代码运行后
image.png


三.拓展使用
       我们经常是不是会看到利用NPC换装时可以通过按钮或者滑动旋转。这个功能我们也可以通过设置人物的旋转来完成

       我们先看看具体的实现效果
       这里先使用玩家作为对象,如果需要NPC,就把玩家的旋转设置转换成NPC即可。
      

     接下来是代码部分

   

enum RotationState {
        NONE,
        ROTATE_LEFT,
        ROTATE_RIGHT,
}
export default class UIRotation extends UI.UIBehavior {
        /**旋转的状态 */
        private _rotationState: RotationState = RotationState.NONE;
        onStart() {
                this.canUpdate = true;
                let mButtonLeft = this.uiWidgetBase.findChildByPath("RootCanvas/mButtonLeft") as UI.Button;
                let mButtonRight = this.uiWidgetBase.findChildByPath("RootCanvas/mButtonRight") as UI.Button;
                mButtonLeft.onPressed.add(() => {
                        this._rotationState = RotationState.ROTATE_LEFT;
                })
                mButtonLeft.onReleased.add(() => {
                        this._rotationState = RotationState.NONE;
                })
                mButtonRight.onPressed.add(() => {
                        this._rotationState = RotationState.ROTATE_RIGHT;
                }
                )
                mButtonRight.onReleased.add(() => {
                        this._rotationState = RotationState.NONE;
                })
        }



        onUpdate(dt: number): void {
                switch (this._rotationState) {
                        case RotationState.ROTATE_LEFT:
                                this.addNPCRotation(5);
                                break;
                        case RotationState.ROTATE_RIGHT:
                                this.addNPCRotation(-5);
                                break;
                        default:
                                break;
                }
        }
        private addNPCRotation(rateRotationZ: number): void {
                let curRotation = Gameplay.getCurrentPlayer().character.worldRotation;
                curRotation.z += rateRotationZ;
                Gameplay.getCurrentPlayer().character.setWorldRotation(curRotation);
        }
}




回复

使用道具 举报

汽汽汽汽水 发表于 2023-8-6 10:29:54 | 显示全部楼层
回复

使用道具 举报

窜稀大仙 发表于 2023-8-6 14:05:38 | 显示全部楼层
NPC.lookAt(target)不香吗
回复

使用道具 举报

猪头BOOM 发表于 2023-8-6 17:05:09 | 显示全部楼层
自觉白嫖 一键三连
回复

使用道具 举报

喵喵哭唧唧 发表于 2023-8-6 18:13:47 | 显示全部楼层
回复

使用道具 举报

热门版块
快速回复 返回顶部 返回列表