Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 24, 2019 at 10:51 AM by adovehv for the following reason:

Other

avatar image
0
Question by adovehv · May 15, 2019 at 02:24 PM · physicsplayerprefabs

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · May 15, 2019 at 03:26 PM 0
Share

Is there a reason you are using that public static Instance pattern for things that will clearly have multiple versions running around?

avatar image adovehv TreyH · May 15, 2019 at 04:09 PM 0
Share

Oops, no, i wrote this for error.

1 Reply

  • Sort: 
avatar image
0

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....

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adovehv · May 16, 2019 at 08:34 AM 0
Share

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

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Character Movement Collision Problem 0 Answers

Changing the color of a block when a player walks on it? 2 Answers

How To Stop Player Completely? 1 Answer

Player object falling through game environment, collision not detected with ground 0 Answers

Sphere get stuck in floor and fails to jump? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges