求一个相机旋转缩放的脚本

用欧拉角旋转很奇怪,要的是轨道相机可以旋转看的。相机位置怎么得到?就是相机和物体的距离角度,这个角度怎么换算的,比方要观察物体在(0,0,0),相机在(0,0,-1)。 那相机的position角度,这个角度的Api是啥?往下的这个位置不知道咋写了?求助

JiaoDu =new Vec3(0,0,0);

distace:number =5;

start () {

    //鼠标监听

    systemEvent.on(SystemEvent.EventType.MOUSE_UP, this.onMouseUp, this);

    systemEvent.on(SystemEvent.EventType.MOUSE_DOWN, this.onMouseDown, this);

    systemEvent.on(SystemEvent.EventType.MOUSE_MOVE,this.onMouseMove,this);

    

    //触摸监听

    systemEvent.on(SystemEvent.EventType.TOUCH_MOVE,this.onTouchMove,this);

}

isDown: boolean = false;

onMouseDown(e: EventMouse) {

    this.isDown = true;

}

onMouseUp(e: EventMouse) {

    this.isDown = false;

}

onMouseMove(event:EventMouse){  

    if(this.isDown ==true){

        if(event.movementX != 0) {

          this.JiaoDu.y+=event.movementX;

        }

        if(event.movementY != 0) {

            this.JiaoDu.x+=event.movementY;

        }

        this.node.eulerAngles =this.JiaoDu;

    }  

  

}

你想要的是这个功能吗?
3d
论坛有贴子提过,官方demo里也有

1赞

是的,我下了3D的两个合集没看到啊,我再找找,非常感谢

哪个案例呀?????同求

说的应该是创建项目时,有选的那个模版

找不到…给点提示吧兄弟


不一样啊,没脚本就一个模型

找不到。。。。这就尴尬了

这个是hello word 模板,我改动了一下,我直接贴代码把
import { _decorator, Component, Node, Vec3, Quat, EventTouch, log, CCLoader, Camera, EventMouse } from ‘cc’;

const { ccclass, property } = _decorator;

@ccclass('Camera3dMgr')

export class Camera3dMgr extends Component {

    @property(Camera)

    private camera3d: Camera = null;

    @property(Node)

    private targetNode: Node = null;

    onEnable() {

        console.log(this)

        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);

        this.node.on(Node.EventType.MOUSE_WHEEL, this.onMousewheel, this);

    }

    onDisable() {

        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);

        this.node.off(Node.EventType.MOUSE_WHEEL, this.onMousewheel, this);

    }

    private _curPos = new Vec3;

    private _curRot = new Quat;

    private onTouchMove(event: EventTouch) {

        this.camera3d.node.getWorldPosition(this._curPos);

        this.camera3d.node.getWorldRotation(this._curRot);

        const delta = event.getDelta();

        const dx = delta.x / 300; 

        const dy = delta.y / 300;

        console.log(delta);

        const rot = new Quat(this._curRot);

        const euler = new Vec3();

        Quat.rotateX(rot, rot, dy);

        Quat.rotateAround(rot, rot, Vec3.UNIT_Y, -dx);

        Quat.toEuler(euler, rot);

        Quat.fromEuler(rot, euler.x, euler.y, 0); 

        this._rot = rot;

        this.setVisualAngle(this._rot, this._depth);

    }

    private _rot: Quat = new Quat;

    private _depth: number = 15;

    private onMousewheel(event: EventMouse) {

        let scrolly = event.getScrollY();

        if (scrolly > 0) {

            this._depth += 1

        } else {

            this._depth -= 1

        }

        this.setVisualAngle(this._rot, this._depth);

        console.log(scrolly)

    }

    private setVisualAngle(rot: Quat, depth: number) {

        const offset = new Vec3(0, 0, 1);

        Vec3.transformQuat(offset, offset, rot);

        Vec3.normalize(offset, offset);

        Vec3.multiplyScalar(offset, offset, depth); 

        Vec3.add(this._curPos, this.targetNode.worldPosition, offset);

        this.camera3d.node.setWorldPosition(this._curPos);

        const up = new Vec3(0, 1, 0);

        Vec3.transformQuat(up, up, rot);

        Vec3.normalize(up, up);

        this.camera3d.node.lookAt(this.targetNode.worldPosition, up);

    }

}
1赞

这里确实没脚本,忘了哪里看到的了…

兄弟你的是什么版本,为什我的没有,感谢不厌其烦的解答,太友好了

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。