C# 剑碰撞计时
因此,我制作了一个剑形碰撞脚本,它可以启用动画并触发长方体碰撞器,是否可以为动画添加计时器并延长长方体碰撞器的时间C# 剑碰撞计时,c#,unity3d,frame-rate,C#,Unity3d,Frame Rate,因此,我制作了一个剑形碰撞脚本,它可以启用动画并触发长方体碰撞器,是否可以为动画添加计时器并延长长方体碰撞器的时间 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Sword : MonoBehaviour { Animator anim; Collider Col; private void Start() {
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;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级之剑:单一行为
{
动画师;
对撞机;
私有void Start()
{
anim=GetComponent();
Col=GetComponent();
}
私有void更新()
{
if(Input.GetButtonDown(“Fire1”))
动画挫折(“攻击”,真实);
else if(Input.GetButtonUp(“Fire1”))
动画挫折(“攻击”,假);
if(Input.GetButtonDown(“Fire1”))
Col.enabled=true;
if(Input.GetButtonUp(“Fire1”))
Col.enabled=false;
}
}
您可以使用协同程序在更新循环之外执行操作
float enableColliderForSeconds = 1f;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Col.enabled = false; //in case it is enabled from a canceled Coroutine
StopCoroutine(Collider()); //canceling Coroutine first to not stack
StartCoroutine(Collider());
}
}
IEnumerator Collider()
{
Col.enabled = true;
yield return new WaitForSeconds(enableColliderForSeconds);
Col.enabled = false;
}
您可能应该更详细地了解您真正想要做的事情,因为这确实不适用于适当的游戏机制。您需要单独跟踪碰撞器已激活多长时间,然后在该时间结束后禁用它 有很多方法可以做到这一点,下面是一个简单的例子:
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;
}
}
动画师动画;
对撞机;
浮动时间=1.5f;
float-collitterimeremaining=0;
私有void Start()
{
anim=GetComponent();
Col=GetComponent();
}
私有void更新()
{
如果(列启用){
CollidaterTimeMining-=Time.deltaTime;
如果(对撞机对撞机对撞机对撞机从你的问题中我无法理解你想做什么。请更详细地解释你的意思。因此,当我攻击长方体对撞机时,只有当鼠标左键按下时,对撞机才会出现,当我放开对撞机时,对撞机就不再对撞,我试图找出答案,因此,如果你点击鼠标左键,对撞机就会消失llider在特定时间段内打开,然后在该时间段后关闭,因此这将是一个跟踪主要攻击脚本的单独脚本,对吗?不,这将替换问题中的您自己的脚本。而不是在玩家按下或释放按钮时将其设置为开/关-在玩家按下按钮时将其设置为开,然后将其关闭呃,某个时间已经过去了,感谢它的作品,只是动画在循环,但我可以修复好,谢谢你,这修复了一切,我只是在脚本中添加了一个额外的部分,告诉动画在我松开按钮时停止,但这就是我所要修复的一切,一切正常。