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 /
  • Help Room /
avatar image
0
Question by jefskers · Nov 29, 2017 at 11:03 AM · collisioncoroutineontriggerentermissiletanks

Missile Boucne Issues (need a creative solution)

Im pretty new to unity, making a simple tank game. The problem is with the wall bounce where i wanted to have the missile bounce off a wall only once and destroy it when it hit a wall the second time which i did with a co routine that i call in update. Missiles are also supposed to destroy each other and other players no matter what but every so often they will:

A) bounce more than once off of walls (usually just twice and then explode but sometimes even more than that.)

B) Not destroy the player on a hit after a bounce.

C) bounce off another missile instead of destroying it.

The wierd thing is that it seems to be completely random. I think it has something to do with the co routine but i'm new to unity and cant make out what the problem is and couldnt find anything else helpful on the web.

   using UnityEngine;
     using System.Collections;
     
     public class MissileBounce : MonoBehaviour {
     
         public float speed = 20;
         public RaycastHit hit;
         public GameObject explosion;
     
         void Start ()
         {
             GetComponent<Rigidbody> ().velocity = transform.forward * speed;
         }
     
         void Update ()
         {
             Ray ray = new Ray (transform.position, transform.forward);
     
             if (Physics.Raycast (ray, out hit, Time.deltaTime * speed + .1f))
                 StartCoroutine (Bounce ());
         }
     
             
         public IEnumerator Bounce ()
         {
             Ray ray = new Ray (transform.position, transform.forward);
     
     
             Vector3 reflectDir = Vector3.Reflect (ray.direction, hit.normal);
             float rot = 90 - Mathf.Atan2 (reflectDir.z, reflectDir.x) * Mathf.Rad2Deg;
             transform.eulerAngles = new Vector3 (0, rot, 0);
             speed = 20;
             GetComponent<Rigidbody> ().velocity = transform.forward * speed;    
     
             yield return new WaitUntil (SecondBounce);
                 explosion = Instantiate(explosion, transform.position, transform.rotation) as GameObject;
                 Destroy (gameObject);
         }
     
         bool SecondBounce()
         {
             Ray ray = new Ray (transform.position, transform.forward);
     
             if (Physics.Raycast (ray, out hit, Time.deltaTime * speed + 1f))
                 return true;
             else
                 return false;
             
         }
 
 }

this script controls the bounce of the missile off a wall and waits until the second hit to destroy it

 using UnityEngine;
 using System.Collections;
 
 public class HitControl : MonoBehaviour {
 
     public GameObject explosion;
     public GameObject playerExplosion;
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Missile")
         {
             explosion = Instantiate (explosion, transform.position, transform.rotation) as GameObject;
             Destroy (transform.parent.gameObject);
             Destroy (gameObject);
             Destroy (other.gameObject);
             Debug.Log("this wierd");
 
         }
         if (other.tag == "Player")
         {
             playerExplosion = Instantiate (playerExplosion, transform.position, transform.rotation) as GameObject;
             Destroy(other.gameObject);
             Destroy (transform.parent.gameObject);
             Destroy (gameObject);
         }
     }
 }

The idea here is that i made an empty gameobject the child of the missile and put the collider on that to control collisions while the script on missile just controls the bounce off the wall. when i had them both on the same script it didnt work. Any insight to how I can solve those above three problems is well appreciated cuz ive been stuck on this for a few days.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

165 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

Related Questions

Coroutines WaitForSeconds question... 3 Answers

Lose Health when GameObject enters collider? (OnTriggerEnter) 2 Answers

Function OnTriggerEnter2D is not called 1 Answer

Make object(s) stack vertically. 0 Answers

Trigger not detected 0 Answers


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