[英]Unity 3d, BoxCollider for sword
So I'm working on a First-person sword game, I have the basic fighting setup but if you walk into an enemy they take damage from the collider, I want to have the Collider disabled until I use my attack button and then go back to disabled so you can't just walk into enemies.所以我正在开发一个第一人称剑游戏,我有基本的战斗设置,但是如果你走进一个敌人,他们会受到对撞机的伤害,我想禁用对撞机,直到我使用我的攻击按钮然后 go 回来禁用所以你不能只是走进敌人。 (C#, Unity 2020.2, 3d) (C#, Unity 2020.2, 3d)
Here is the sword Animation Script which I would like to also have the Box Collider change in this as well for simple accessibility.这是剑 Animation 脚本,我也想在其中更改 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);
}
}
You can get access to your Collider the same way you got your Animator and after that, you can do collider.enabled = false
to disable it.您可以像获得 Animator 一样访问 Collider,之后,您可以执行collider.enabled = false
来禁用它。
You can also go there to read more about colliders: https://docs.unity3d.com/ScriptReference/Collider.html You can also go there to read more about colliders: https://docs.unity3d.com/ScriptReference/Collider.html
So with the code there is an animator attached and a Collider attached to the script, when left clicked the animation will play and the collider will turn on因此,在代码中附加了一个动画师和一个附加到脚本的碰撞器,当左键单击时,animation 将播放并且碰撞器将打开
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.