C# Unity 3d,用于剑的BoxCollider

C# Unity 3d,用于剑的BoxCollider,c#,unity3d,frame-rate,C#,Unity3d,Frame Rate,所以我正在做一个第一人称剑类游戏,我有基本的战斗设置,但是如果你遇到敌人,他们会受到对撞机的伤害,我想让对撞机禁用,直到我使用我的攻击按钮,然后返回到禁用状态,这样你就不能只碰到敌人。(C#,Unity 2020.2,3d) 这里是剑动画脚本,我想在这也有简单的可访问性框碰撞器的变化 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Sword : MonoBe

所以我正在做一个第一人称剑类游戏,我有基本的战斗设置,但是如果你遇到敌人,他们会受到对撞机的伤害,我想让对撞机禁用,直到我使用我的攻击按钮,然后返回到禁用状态,这样你就不能只碰到敌人。(C#,Unity 2020.2,3d)

这里是剑动画脚本,我想在这也有简单的可访问性框碰撞器的变化

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

public class Sword : MonoBehaviour
{
     Animator anim;

     private void Start()
    {
        anim = GetComponent<Animator>();
    }
   private void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        anim.SetBool("Attacking", true);
        else if(Input.GetButtonUp("Fire1"))
        anim.SetBool("Attacking", false);
    }
    
 
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级之剑:单一行为
{
动画师;
私有void Start()
{
anim=GetComponent();
}
私有void更新()
{
if(Input.GetButtonDown(“Fire1”))
动画挫折(“攻击”,真实);
else if(Input.GetButtonUp(“Fire1”))
动画挫折(“攻击”,假);
}
}

您可以使用与获得动画师相同的方式访问碰撞器,然后执行
Collider.enabled=false
以禁用碰撞器


您还可以到那里阅读有关碰撞器的更多信息:

因此,代码中有一个动画师和一个碰撞器连接到脚本,当左键单击时,动画将播放,碰撞器将打开

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;
}

好的,我会试试这个,谢谢。我还看到你在更新中处理输入,我建议你去看看输入操作,它们是处理输入的非常好的方法。因此我添加了collider.enabled=False我将此添加到更新函数中,col正在获取BoxCollider的组件,但无论发生什么情况,BoxCollider都会保持不变t、 我只希望它在单击时显示。if(Input.GetButtonDown(“Fire1”))Col.enabled=true;if(Input.GetButtonDown(“Fire1”))Col.enabled=false;不管我键入了什么错误