![](/img/trans.png)
[英]How to detect collisions when using a GameObject collides with a BoxCollider 2D while being dragged on Unity 3D
[英]Unity 3d, BoxCollider for sword
所以我正在开发一个第一人称剑游戏,我有基本的战斗设置,但是如果你走进一个敌人,他们会受到对撞机的伤害,我想禁用对撞机,直到我使用我的攻击按钮然后 go 回来禁用所以你不能只是走进敌人。 (C#, Unity 2020.2, 3d)
这是剑 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);
}
}
您可以像获得 Animator 一样访问 Collider,之后,您可以执行collider.enabled = false
来禁用它。
You can also go there to read more about colliders: https://docs.unity3d.com/ScriptReference/Collider.html
因此,在代码中附加了一个动画师和一个附加到脚本的碰撞器,当左键单击时,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.