cocos球面移动

import { _decorator, CapsuleCharacterController, CapsuleCollider, Component, Director, director, EventKeyboard, EventMouse, find, Input, input, ITriggerEvent, KeyCode, Node, PhysicsSystem, RigidBody, v3, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('t1')

export class t1 extends Component {

    x1:number = 0

    z1:number = 0

    s1:number = 5

    n1:number = 0

    n2:number = 0

    g1:number = 0

    g2:number = 0

    k1:number = 0

    k2:number = 0

    o1:Node = null

    o2:Node = null

    w1:boolean = false

    p1 = v3()

    u1:boolean = false

   

    r1 = v3()

    start() {

        this.o1 = find('e1')

    }

    onLoad (){

        input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this)

        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)

        input.on(Input.EventType.KEY_PRESSING, this.onKeyDown, this)

        input.on(Input.EventType.KEY_UP, this.onKeyUp, this)

    }

    onDestroy (){

        input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this)

        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this)

        input.off(Input.EventType.KEY_PRESSING, this.onKeyDown, this)

        input.off(Input.EventType.KEY_UP, this.onKeyUp, this)

    }

    onKeyDown(event: EventKeyboard) {

        if (event.keyCode == KeyCode.KEY_W) {

            this.z1 = -1

        }

        if (event.keyCode == KeyCode.KEY_A) {

            this.x1 = -1

        }

        if (event.keyCode == KeyCode.KEY_S) {

            this.z1 = 1

        }

        if (event.keyCode == KeyCode.KEY_D) {

            this.x1 = 1

        }

    }

    onKeyUp(event: EventKeyboard) {

        if (event.keyCode == KeyCode.KEY_W) {

            this.z1 = 0

            this.k1 = this.node.position.y

        if(this.node.position.y < 5002){this.k1 = 5002}

        this.node.setPosition(0,this.k1,0)

        this.node.setRotationFromEuler(0,0,0)

        this.o1.setRotationFromEuler(this.o1.eulerAngles.x + -this.n2,0,this.o1.eulerAngles.z + -this.n1)

        }

        if (event.keyCode == KeyCode.KEY_A && this.x1 == -1) {

            this.x1 = 0

        }

        if (event.keyCode == KeyCode.KEY_S) {

            this.z1 = 0

        }

        if (event.keyCode == KeyCode.KEY_D && this.x1 == 1) {

            this.x1 = 0

        }

    }

    onMouseMove(event: EventMouse) {

        if (event.getButton() == 2) {

            //获取本地坐标系下的旋转,用欧拉角表示

            let fe1 = this.node.getChildByName('c2').eulerAngles.x

            let fe2 = this.node.getChildByName('c2').eulerAngles.y

            //鼠标在UI坐标系下的移动距离

            let x1 = event.movementX * 0.5

            let y1 = event.movementY * 0.5

            //用欧拉角设置世界坐标系下的旋转

            fe1 += -y1

            fe2 += -x1

            if (fe1 > -80 && fe1 < 80) {

                this.node.getChildByName('c2').setRotationFromEuler(fe1, fe2,0)

            }

        }

    }

    update(deltaTime: number) {

        this.r1 = new Vec3(this.x1 * this.s1 * deltaTime, 0, this.z1 * this.s1 * deltaTime)

        Vec3.transformQuat(this.r1, this.r1, this.node.getChildByName('c2').getRotation())

        this.p1 = v3(this.r1.x, -0.2,this.r1.z)

        this.node.getComponent(CapsuleCharacterController).move(this.p1)

        //this.node.getComponent(RigidBody).setLinearVelocity(this.p1)

        //给Z轴

        this.g1 = Math.atan2(this.node.position.x,this.node.position.y)

        this.n1 = this.g1 * 180 / Math.PI * -1

        //给X轴

        this.g2 = Math.atan2(this.node.position.z,this.node.position.y)

        this.n2 = this.g2 * 180 / Math.PI

        this.node.setRotationFromEuler(this.n2,0,this.n1)

        //console.log(g1 + ' aaa ')

        //console.log(this.n1 + ' aaa ')

        //console.log(g2 + ' aaa ')

        //console.log(this.n2 + ' aaa ')

    }

}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值