Cocos Creator 围绕目标物体旋转(注:Demo是cocos3.8.0 编辑器,其余版本还请读者自行适配)

把下面的ts脚步拖拽到canvas节点上(并把目标3d节点拖拽到SlideSideToSide装饰器上)

然后添加widget组件,并设置成如下参数

然后保存场景即可预览

import { _decorator, Component, Touch, find, Node, NodeEventType, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SlideSideToSide')
export class SlideSideToSide extends Component {

//第 0 步 以下

@property({ displayName: '目标节点', tooltip: "旋转中心点", type: Node })

targetPositionNode: Node;

//第 0 步 以上是最开始

//下面的属性是用到的时候才声明


    public static Instance: SlideSideToSide = null;
    private theta: number = 0;
    private curTheta: number = 0;
    private phi: number = 0;
    private curPhi: number = 0;
    private rotateSpeedHorizontal = 0.001;
    private rotateSpeedVertical = 0.001;
    private camera: Node;
    private distance: number;
    private cameWorldPos: Vec3;
    private x: number;
    private y: number;
    private z: number;
    private num = 0.08;
    private namePos: string;
    private angleStart: number;
    private deltaStart: Vec2;
    private deltaMove: Vec2;
    private initCameraPos: Vec3;
    private isRotate = false;
    private targetPosition: Vec3;

//第(8)步

    constructor() {
        super();
        if (SlideSideToSide.Instance === null) {
            SlideSideToSide.Instance = this;
        } else {
            this.destroy;
            return;
        }
    }

    onLoad() {

//第(6)步
        this.camera = find("Main Camera");

//第(5)步
        this.updateTargetCenter(this.targetPositionNode);
    }

//第(1)步

    protected start(): void {
        this.registerTouchEvent();
    }

    updateTargetCenter(targetCenter: Node) {
        this.initCameraPos = this.camera.getWorldPosition();
        this.cameWorldPos = this.camera.getWorldPosition();
        const delta = targetCenter.getWorldPosition().subtract(this.camera.getWorldPosition());

        this.distance = delta.length();

        this.theta = Math.atan2(-delta.z, -delta.x);
        this.phi = Math.acos(-delta.y / this.distance);
        
        this.camera.lookAt(targetCenter.getWorldPosition());
        this.targetPosition = targetCenter.getWorldPosition();
        this.updateRotation(0, 0);
    }

//第(4)步

    updateRotation(deltaTheta: number, deltaPhi: number) {
        // 根据 delta 增量计算水平和垂直角度变化量
        deltaTheta = deltaTheta * this.rotateSpeedHorizontal;
        deltaPhi = deltaPhi * this.rotateSpeedVertical;

        // 更新水平和垂直旋转角度
        this.curTheta = this.theta + deltaTheta;
        this.curPhi = Math.max(0, Math.min(Math.PI, this.phi + deltaPhi));

        // 根据距离和当前水平与垂直夹角更新摄像机位置和朝向
        this.updateObjectRotation();

        // 更新摄像机水平和垂直旋转角度
        this.theta = this.curTheta;
        this.phi = this.curPhi;

    }

//第(4)步

    updateObjectRotation() {
        const delta = this.camera.getWorldPosition().subtract(this.targetPosition);
        this.distance = delta.length();

        if (this.phi > Math.PI / 2.001) this.phi = Math.PI / 2.001;      //水平旋转删除这两行
        else if (this.phi < 0.001)        //水平旋转删除这两行

 this.phi = 0.001;
        this.x = this.distance * Math.sin(this.phi) * Math.cos(this.theta);
        this.y = this.distance * Math.cos(this.phi);
        this.z = this.distance * Math.sin(this.phi) * Math.sin(this.theta);
        this.camera.setWorldPosition(this.targetPosition.x + this.x, this.targetPosition.y + this.y, this.targetPosition.z + this.z);
        this.camera.lookAt(this.targetPosition);
    }

//第(3)步

    onTouch_Start(event: Touch) {
        this.targetPosition = this.targetPositionNode.getWorldPosition();
    }

    // 移动Node
    onTouch_Move(event: Touch) {
        const delta = event.getDelta();
        this.updateRotation(delta.x, delta.y);
    }

    onTouch_Cancel() {

    }

//第(2)步

    registerTouchEvent() {
        this.node.on(NodeEventType.TOUCH_START, this.onTouch_Start, this);
        this.node.on(NodeEventType.TOUCH_MOVE, this.onTouch_Move, this);
        this.node.on(NodeEventType.TOUCH_END, this.onTouch_Cancel, this);
        this.node.on(NodeEventType.TOUCH_CANCEL, this.onTouch_Cancel, this);
    }

//第(7)步

    onDestroy() {
        this.node.on(NodeEventType.TOUCH_START, this.onTouch_Start, this);
        this.node.on(NodeEventType.TOUCH_END, this.onTouch_Cancel, this);
        this.node.on(NodeEventType.TOUCH_CANCEL, this.onTouch_Cancel, this);
    }
}

//当按我的步骤打的时候会爆红,是因为相关的变量没声明,或者没在cc里面倒入;如果遇到不懂//的可以问一下gtp或者文心一言

//温馨提示:有时候它给的说法并不一定准确,大家要有辨别是非的能力,包括gtp。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator编辑器提供了一套强大的功能,使开发者可以轻松地编写工具来增强开发流程。以下是Cocos Creator编辑器如何编写工具的步骤: 1.熟悉Cocos Creator编辑器:首先,开发者需要熟悉Cocos Creator编辑器的基本操作和功能。这包括了场景编辑、节点操作、组件置等。 2.了解Cocos Creator的插件机制:Cocos Creator提供了插件机制,开发者可以通过插件扩展编辑器的功能。开发者可以在官方文档中找到关于插件开发的详细说明。 3.确定工具的需求:开发者需要明确工具的需求和目标。这可以是一项新功能、自动化操作、场景批量处理等。确保明确工具的功能和预期效果。 4.开发插件:根据工具的需求,开发者可以通过插件机制来实现工具。插件可以包括自定义的编辑器窗口、面板、节点操作脚本等。 5.使用API扩展功能:Cocos Creator提供了丰富的API来扩展编辑器的功能。开发者可以使用这些API来实现工具的核心功能。 6.测试和调试:在编写工具过程中,开发者需要进行反复的测试和调试以确保工具的正确性和可靠性。这可以包括单元测试、集成测试和用户界面测试等。 7.发布和分享工具:完成工具开发后,开发者可以考虑将其发布和分享。开发者可以将工具打包成插件,供其他开发者使用和安装。 总之,使用Cocos Creator编辑器编写工具可以有效地提高开发效率和质量。通过插件机制和丰富的API,开发者可以根据自己的需求自定义和扩展编辑器的功能。希望这些步骤能对您编写Cocos Creator编辑器工具有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值