微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

剑碰撞时机

如何解决剑碰撞时机

所以我制作了一个剑碰撞脚本来启用动画并触发盒子碰撞器,无论如何要为动画添加一个计时器并延长盒子碰撞器的时间。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sword : MonoBehavIoUr
{
    Animator anim;
    Collider Col;

    private void Start()
    {
        anim = GetComponent<Animator>();
        Col = GetComponent<BoxCollider>();
    }
    private void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        anim.SetBool("Attacking",true);
        else if(Input.GetButtonUp("Fire1"))
        anim.SetBool("Attacking",false);

        if (Input.GetButtonDown("Fire1"))
        Col.enabled = true;
        if (Input.GetButtonUp("Fire1"))
        Col.enabled = false;
    }

}

解决方法

您可以使用协程在更新循环之外执行操作。

    const ytdl = require("ytdl-core");
    var servers = {};
    
    client.on('message',message => {
        
        let args = message.content.substring(PREFIX.length).split(" ");
        switch (args[0]){
            case "play":
                const prefix = '!'; 
            function play(connection,message){
            var server= servers[message.guild.id];
                server.dispatcher = connection.play(ytdl(server.queue[0],{filter: "audioonly"}));
    
                server.queue.shift();
                server.dispatcher.on("end",function(){
                    if(server.queue[0]){
                        play(connection,message);
                    }else {
                        connection.disconnect();
    
                    }
                });
            }
    
           if(!args[1]){
               message.channel.send("you need to provide a link");
               return;
           }
           if(!message.member.VoiceChannel){
               message.channel.send("you must be in a channel to play the bot");
            return;
            }
           
            if(!serveres[message.guild.id]) servers[message.guild.id] = {
    
               queue: [] 
            }
           var server = servers[message.guild.id];
    
           server.queue.push(args[1]);
    
            if(!message.guild.voice) message.member.VoiceChannel.join().then(function(Connection){
                play(connection,message)
    
            })
    
           break;
    
           case 'skip':
               var server = servers[message.guild.id];
               if(server.dispatcher) server.dispatcher.end();
               break;
               case 'stop':
                var server = servers[message.guild.id];
                if(message.guild.voice){
                    for(var i = server.queue.length -1; i >=0;i--){
                    server.queue.splice(i,1);   
                    }
                    server.dispatcher.end();
                    console.log('stopped the queue')
                }
                if(message.guild.connection) message.guild.voice.disconnect();
                break;
        }
    
    
    });

您可能应该更详细地弄清楚您真正想要做什么,因为这确实不适用于适当的游戏机制。

,

您需要单独跟踪对撞机活动的时间,并在该时间结束后禁用它。

有很多方法可以做到这一点,这里有一个简单的例子:

    Animator anim;
    Collider Col;

    float colliderAppearTime = 1.5f;
    float colliderTimeRemaining = 0;

    private void Start()
    {
        anim = GetComponent<Animator>();
        Col = GetComponent<BoxCollider>();
    }
    private void Update()
    {
        if (Col.enabled) {
            colliderTimeRemaining -= Time.deltaTime;
            if (colliderTimeRemaining <= 0) {
                Col.enabled = false;
            }
        }

        if (Input.GetButtonDown("Fire1")) {
            anim.SetBool("Attacking",true);
            Col.enabled = true;
            colliderTimeRemaining = colliderAppearTime;
        }
    }

当按下开火按钮时,我们启用碰撞器并将 colliderTimeRemaining 变量重置为我们希望它停留的时间 - 在这种情况下,我们使用 colliderAppearTime 变量设置为 1.5 秒.

然后,每一帧,我们检查是否启用了 colider。如果是,我们将 colliderTimeRemaining 减少帧时间 deltaTime,当它达到 0 时,我们关闭碰撞器。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。