- Home /
Other
Same duplicate prefab not work.
Hi! I have a arrow trap, when the player collider collision with the trap collider, the trap is activate and throw one arrow, but if i put two arrow traps prefabs, only work one. Where is the problem? Thanks.
public class ArrowTrap : MonoBehaviour {
public static ArrowTrap Instance;
public float speed;
public GameObject arrowPrefab;
public Arrow arrowScript;
BoxCollider2D boxCollider2D;
void Start()
{
Instance = this;
GameObject g = GameObject.FindGameObjectWithTag("arrow");
boxCollider2D = GetComponent<BoxCollider2D>();
arrowScript = g.GetComponent<Arrow>();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.transform.tag == "Player")
{
arrowScript.arrowTriggered = true;
Debug.Log("TRIGGER WORK");
boxCollider2D.enabled = false;
}
}
}
public class Arrow : MonoBehaviour
{
public static Arrow Instance;
public float speed;
GameObject player;
Rigidbody2D rb2d;
Vector3 dir;
Animator anim;
int arrowDmg;
BoxCollider2D boxCollider2D;
public SpriteRenderer arrowObj;
public Player playerScript;
void Start()
{
Instance = this;
player = GameObject.FindGameObjectWithTag("Player");
rb2d = GetComponent<Rigidbody2D>();
arrowDmg = 1;
boxCollider2D = GetComponent<BoxCollider2D>();
anim = GetComponent<Animator>();
arrowObj = GetComponent<SpriteRenderer>();
GameObject g = GameObject.FindGameObjectWithTag("Player");
playerScript = g.GetComponent<Player>();
}
public bool arrowTriggered = false;
void FixedUpdate()
{
if (arrowTriggered == false)
{
rb2d.constraints = RigidbodyConstraints2D.FreezeAll;
}
if (arrowTriggered == true)
{
rb2d.MovePosition(transform.position + (dir * speed) * Time.deltaTime);
// Lanzar flecha
if (player != null)
{
dir = (new Vector2(0, -1)).normalized;
print("SHOOT ARROW");
rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
}
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.transform.tag == "Player")
{
playerScript.vulnerability = true;
Player.instance.CallFlashCoCoroutine();
anim.SetBool("destroy", true);
arrowTriggered = false;
Health.Instance.health -= arrowDmg;
boxCollider2D.enabled = false;
Debug.Log("ARROW COLLISION PLAYER");
}
if (col.transform.tag == "wallsArrow")
{
anim.SetBool("inWall", true);
arrowTriggered = false;
Destroy(boxCollider2D, 0.01f);
arrowObj.GetComponent<Renderer>().sortingLayerName = "LevelDown";
Debug.Log("ARROW IN WALL");
}
}
}
Is there a reason you are using that public static Instance pattern for things that will clearly have multiple versions running around?
Answer by unity_21erushbrook · May 15, 2019 at 08:44 PM
My best guess is that they are in each other because they are firing at the same time. Use a coroutine to space them apart by a few milliseconds.
Also, why are you multiplying things by Time.deltaTime
in a FixedUpdate()
loop? Use Time.fixedDeltaTime
. FixedUpdate calls at a set rate (usually 50/s)
I also recommend having the arrow move on Start (which will just start moving it when it spawns). This will mitigate the need for the allowTriggered bool which is called to move the arrow.
If you would like I can make you a functional arrow prefab....
Hi! The arrow trap work a bit different, the arrow is in a wall always, and when the player cross, the arrow is activated and only shoot one arrow, i have this video to show you how it work and you can see the problem (only work the last prefab put in scene) . Thanks :)
https://www.youtube.com/watch?v=TB1m1Ce2Z$$anonymous$$Y&feature=youtu.be