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.
Your answer
Follow this Question
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