角色动作切换

import { _decorator, Animation, AnimationClip, CharacterController, Component, EventKeyboard, EventMouse, Input, input, KeyCode, Node, RigidBody, v2, v3, Vec2, Vec3 } from 'cc';

const { ccclass, property } = _decorator;



 

@ccclass('c1')

export class c1 extends Component {

    sp1: number = 2

    res1: Vec3 = v3(0, 0, 0)

    x1: number = 0

    z1: number = 0

    characterController: CharacterController = null

    g1:Animation = null

    g2:AnimationClip = null

    g3:AnimationClip = null

    g4:AnimationClip = null

    g5:AnimationClip = null

    g6:AnimationClip = null

       

    start() {

        this.characterController = this.node.getComponent(CharacterController)

        //旋转视角

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

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

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

        this.g1 = this.node.getChildByName('c1').getComponent(Animation)

        this.g2 = this.g1.clips[7]

        this.g3 = this.g1.clips[5]

        this.g4 = this.g1.clips[2]

        this.g5 = this.g1.clips[3]

        this.g6 = this.g1.clips[4]

    }

    onKeyDown(event: EventKeyboard) {

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

            this.z1 = 1

            this.g1.getState(this.g2.name).play()

        }

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

            this.x1 = 1

            this.g1.getState(this.g4.name).play()


 

        }

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

            this.z1 = -1

            this.g1.getState(this.g6.name).play()

        }

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

            this.x1 = -1

            this.g1.getState(this.g5.name).play()

        }



 

    }

    onKeyUp(event: EventKeyboard) {

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

            this.z1 = 0

            this.g1.getState(this.g3.name).play()

            if(this.x1 != 1 ){

            this.g1.getState(this.g4.name).stop()

            }

            if(this.x1 != -1 ){

            this.g1.getState(this.g5.name).stop()

            }

            this.g1.getState(this.g6.name).stop()

           

            this.g1.getState(this.g2.name).stop()

        }

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

            this.x1 = 0

            this.g1.getState(this.g3.name).play()

            this.g1.getState(this.g4.name).stop()

            this.g1.getState(this.g5.name).stop()

            if(this.z1 != -1 ){

            this.g1.getState(this.g6.name).stop()

            }

            if(this.z1 != 1 ){

            this.g1.getState(this.g2.name).stop()

            }

        }

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

            this.z1 = 0

            this.g1.getState(this.g3.name).play()

            if(this.x1 != 1 ){

            this.g1.getState(this.g4.name).stop()

            }

            if(this.x1 != -1 ){

            this.g1.getState(this.g5.name).stop()

            }

            this.g1.getState(this.g6.name).stop()

            this.g1.getState(this.g2.name).stop()

        }

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

            this.x1 = 0

            this.g1.getState(this.g3.name).play()

            this.g1.getState(this.g4.name).stop()

            this.g1.getState(this.g5.name).stop()

            if(this.z1 != -1 ){

            this.g1.getState(this.g6.name).stop()

            }

            if(this.z1 != 1 ){

            this.g1.getState(this.g2.name).stop()

            }

        }

    }

    //旋转视角

    onMouseMove(event: EventMouse) {

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

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

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

            let fe2 = this.node.getChildByName('n2').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('n2').setRotationFromEuler(fe1, fe2, 0)

            }

        }

    }

    update(deltaTime: number) {

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

        this.res1 = new Vec3(this.x1 * this.sp1 * deltaTime, 0, this.z1 * this.sp1 * deltaTime);

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

        Vec3.transformQuat(this.res1, this.res1, this.node.getChildByName('c1').getRotation());

        this.characterController.move(v3(this.res1.x, -0.2, this.res1.z));

        if(this.z1 !=0 || this.x1 != 0){

        let h1 = this.node.getChildByName('n2')

        let h2 = this.node.getChildByName('c1')

        h2.setRotationFromEuler(h2.eulerAngles.x,h1.eulerAngles.y+180,h2.eulerAngles.z)

        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值