问题描述
所以我正在开发第一人称剑术游戏,我有基本的战斗设置,但是如果你走进一个敌人,他们会受到碰撞器的伤害,我想禁用碰撞器直到我使用我的攻击按钮并且然后回到禁用状态,这样你就不能只是走进敌人。 (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;
}