cocos creator 笔记-模型转动

cocos creator第三人称视角下查看模型

编译器版本 3.5.2

监听键盘和滚轮事件(使用wasd转动模型,滚轮缩放)

    static selfAction(node:Node) {

        input.on(Input.EventType.KEY_DOWN, (event: EventKeyboard)=>{
            if (commonUtil.actionState) {
                viewUtil.viewMode(node,event,this.rotax,this.rotay)
            }
        }, this);

        input.on(Input.EventType.KEY_PRESSING, (event: EventKeyboard)=>{
            if (commonUtil.actionState) {
                viewUtil.viewMode(node,event,this.rotax,this.rotay)
            }
        }, this);

        input.on(Input.EventType.MOUSE_WHEEL, (event: EventMouse)=>{
            if (commonUtil.actionState) {
                scaleUtil.wheelToModelSize(node,event.getScrollY()>0?true:false,1.2)
            }
        }, this);
    }
    static viewMode(node:Node,event: EventKeyboard,rotax:number,rotay: number){
        let rota_target=new Vec3();
        let rota_move = new Quat();
        let rota_cur = new Quat();
        node.getRotation(rota_cur);
        //四元数转欧拉角
        Quat.toEuler(rota_move, rota_cur);
        switch(event.keyCode) {
            case KeyCode.KEY_W:
                if(rota_move.x<=0&&rota_move.x>=-180){
                    if(rota_move.x-rotax<-180){
                        rota_target = new Vec3(rota_move.x-rotax+360,rota_move.y, 0);
                    }else{
                        rota_target = new Vec3(rota_move.x-rotax, rota_move.y, 0);
                    }
                }else{
                    rota_target = new Vec3(rota_move.x-rotax, rota_move.y, 0);
                }
                break;
            case KeyCode.KEY_S:
                if(rota_move.x>=0&&rota_move.x<=180){
                    if(rota_move.x+rotax>180){
                        rota_target = new Vec3(rotax+rota_move.x-360,rota_move.y, 0);
                    }else{
                        rota_target = new Vec3(rota_move.x+rotax, rota_move.y, 0);
                    }
                }else{
                    rota_target = new Vec3(rota_move.x+rotax, rota_move.y, 0);
                }
                break;
            case KeyCode.KEY_A:
                if(rota_move.y<=0&&rota_move.y>=-180){
                    if(rota_move.y-rotay>-180){
                        rota_target = new Vec3(rota_move.x,rota_move.y-rotay+360, 0);
                    }else{
                        rota_target = new Vec3(rota_move.x,rota_move.y-rotay, 0);
                    }
                }else{
                    rota_target = new Vec3(rota_move.x,rota_move.y-rotay, 0);
                }
                break;
            case KeyCode.KEY_D:
                if(rota_move.y<=0&&rota_move.y<=180){
                    if(rota_move.y+rotay>180){
                        rota_target = new Vec3(rota_move.x, rota_move.y+rotay-360, 0);
                    }else{
                        rota_target = new Vec3(rota_move.x, rota_move.y+rotay, 0);
                    }
                }else{
                    rota_target = new Vec3(rota_move.x, rota_move.y+rotay, 0);
                }
                break;
        }
        node.setRotationFromEuler(rota_target);
    }

由于获取到的模型角度有负值,所以需要判断,进行计算。

 static wheelToModelSize(node:Node,wheel:boolean,level:number){
        let scale=new Vec3();
        node.getScale(scale);
        if(wheel){
            node.setScale(scale.x*level,scale.y*level,scale.z*level)
        }else{
            node.setScale(scale.x/level,scale.y/level,scale.z/level)
        }

    }

根据比例进行缩放。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Cocos Creator模拟砸金蛋3d旋转效果 | 附代码egg.zip // Learn TypeScript: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html const {ccclass, property} = cc._decorator; @ccclass export default class Game extends cc.Component { @property Count: number = 5; @property(cc.Prefab) prefab: cc.Prefab = null; @property(cc.Node) nodeParent: cc.Node = null; private mEggs: cc.Node[] = []; // LIFE-CYCLE CALLBACKS: // onLoad () {} start () { } // update (dt) {} onClick(event, data){ switch(data){ case 'add':{ this.addEggs(); break; } case 'move':{ this.moveEggs(); break; } case 'stop':{ this.stopMoveEggs(); break; } } } addEggs(){ if(this.Count <= 0){ return; } this.mEggs = []; const N = 360 / this.Count; for(let i = 0; i < this.Count; i++){ let egg = cc.instantiate(this.prefab); let js = egg.getComponent('Egg'); js.setRadian(i * N * Math.PI / 180); js.updatePos(); egg.parent = this.nodeParent; this.mEggs.push(egg); } } moveEggs(){ for(let i = 0; i < this.mEggs.length; i++){ this.mEggs[i].getComponent('Egg').setMove(true); } } stopMoveEggs(){ for(let i = 0; i < this.mEggs.length; i++){ this.mEggs[i].getComponent('Egg').setMove(false); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暮雪...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值