cocos触摸移动

import { _decorator, Animation, animation, AnimationClip, Collider, color, Color, ColorKey, Component, Director, director, EventKeyboard, EventTouch, find, geometry, ICollisionEvent, input, Input, KeyCode, Node, physics, PhysicsGroup, PhysicsSystem, Quat, RigidBody, Sprite, v2, v3, Vec2, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('pc2')

export class pc2 extends Component {

    private pc1:Node = null

    private pc2:Node = null

    private pc3:Node = null

    private pc4:Node = null

    private ri1:RigidBody = null

    public pb1:Vec2 = v2(0,0)

    public pb2:Vec2 = v2(0,0)

    public w1:Vec3 = v3(0,0,0)

    public w2:Vec3 = v3(0,0,0)

    private max1:number = 50

    private sp1:number = 1.1

    private sp3:number = -2

    private mask:number = 0xffffffff;

    private maxDistance:number = 10000000;

    private queryTrigger:boolean = true

    @property({type:physics.PhysicsGroup})

    group:PhysicsGroup

   

    public onLoad(): void {

        this.pc1 = find('c1/n1/b1')

        this.pc2 = find('c1/n1/b1/b2')

        this.pc3 = find('c1/b3')

        this.pc4 = find('c1/b4')

        this.ri1 = this.node.getComponent(RigidBody)

        this.pc4.on(Input.EventType.TOUCH_START,this.onTouchStart,this)

        this.pc4.on(Input.EventType.TOUCH_MOVE,this.onTouchMove,this)

        this.pc4.on(Input.EventType.TOUCH_CANCEL,this.onTouchEnd,this)

        this.pc4.on(Input.EventType.TOUCH_END,this.onTouchEnd,this)

        this.pc3.on(Node.EventType.TOUCH_MOVE,this.onTouchMove2,this)

    }

    start() {

        //物理过程之后所触发的事件

        director.on(Director.EVENT_AFTER_PHYSICS,this.raycast,this)

    }

    //地面行走射线检测

    raycast(){

        const tmpRay = geometry.Ray.create(this.node.worldPosition.x,this.node.worldPosition.y,this.node.worldPosition.z, 0, -1, 0)

        if( PhysicsSystem.instance.raycastClosest(tmpRay, this.group, 0.5,this.queryTrigger)){

            let ey = PhysicsSystem.instance.raycastClosestResult

            //获取世界坐标系的撞击点

            var y = ey.hitPoint.y

            //获取撞击距离

            var gf1 = ey.distance

            console.log(y + "aaaaa")

            console.log(gf1 +"ddddd")

            this.node.worldPosition = v3(this.node.worldPosition.x,y+0.5,this.node.worldPosition.z)

        }        

    }

    //摇杆背景跟随手指移动

    onTouchStart(event:EventTouch){

        let sc1:Vec2 = event.getUILocation()

        let sc2 = v3(sc1.x,sc1.y,0)

        this.pc1.worldPosition = sc2

    }

    //摇杆按钮移动范围

    onTouchMove(event:EventTouch){

        let sc1:Vec2 = event.getUILocation()

        let sc2 = v3(sc1.x,sc1.y,0)

        //转到屏幕坐标

        let sc3 = v3()

        this.pc1.inverseTransformPoint(sc3,sc2)

        let la1 = sc3.length()

        //范围

        if(la1 > this.max1){

            sc3.x = sc3.x * this.max1 / la1

            sc3.y = sc3.y * this.max1 / la1

            la1 = this.max1

        }

        //方向向量

        this.pb1.x = sc3.x / la1  //cos角度

        this.pb1.y = sc3.y / la1 //sin角度

        this.pc2.setPosition(sc3)

    }

    //旋转视角

    onTouchMove2(event:EventTouch){

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

        let fe1 = this.node.eulerAngles.x

        let fe2 = this.node.eulerAngles.y

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

        let x1 = event.getDeltaX()*0.07

        let y1 = event.getDeltaY()*0.07

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

        fe1+=-y1

        fe2+=x1

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

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

        }

    }

    onTouchEnd(event:EventTouch){

        this.pb1.x = this.pb1.y = 0

        this.pc2.setPosition(0,0,0)

        this.pc1.setPosition(0,0,0)

    }

    update(deltaTime: number) {

   

        // 根据自身方向,转化方向

        const res1 = new Vec3(this.pb1.x,0,-this.pb1.y);

        //获取节点的本地化局部旋转方向,1:返回值,2:需要转换的方向向量,3:当前本地旋转

        Vec3.transformQuat(res1, res1, this.node.getRotation());

        //射线检测

        let js1 = new geometry.Ray(this.node.worldPosition.x,this.node.worldPosition.y,this.node.worldPosition.z,res1.x,0,res1.z)

        if(PhysicsSystem.instance.raycastClosest(js1,this.mask,0.5,this.queryTrigger)){

            const js2 = PhysicsSystem.instance.raycastClosestResult

            const js3 = js2.distance

            //最小距离3

            if(js3<0.5){

                this.ri1.setLinearVelocity(v3(0,0,0))

                return

            }

        }

        //动力学移动

        this.ri1.setLinearVelocity(v3(res1.x*this.sp1,this.sp3,res1.z*this.sp1))

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值