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

Unity 3d,用于剑的 BoxCollider

如何解决Unity 3d,用于剑的 BoxCollider

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

这是剑动画脚本,为了简单的可访问性,我也希望对 Box Collider 进行更改。

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

解决方法

您可以像访问 Animator 一样访问对撞机,之后,您可以执行 collider.enabled = false 禁用它。

您也可以去那里阅读有关碰撞器的更多信息:https://docs.unity3d.com/ScriptReference/Collider.html

,

因此代码中附加了一个动画师和一个附加到脚本的碰撞器,当左键单击时,动画将播放并且碰撞器将打开

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

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