# rocket passing rings

I am creating a simple game in unity 3d where there is a rocket and some rings that will be generated randomly and the rocket has to pass through the rings using the left and right buttons which will turn the rocket left and right.

i have used this script to generate random rings,this is attached to the ring prefab.

``````public GameObject ring;

float counter = 50f;
GameObject clone;

void OnTriggerExit(Collider other)
{
if (other.tag == "Player" && this.gameObject.tag=="ring")
{
Quaternion rot=Quaternion.Euler(0,Random.Range(0,360),0);
Vector3 v = new Vector3 (this.transform.position.x,                      this.transform.position.y, this.transform.position.z + counter);
clone=Instantiate (ring, v, rot) as GameObject;
Destroy(this.gameObject);
}
}
``````

this script places rings one after the other keeping a distance of 50 in between when the rocket passes through the collider and the y axis is given a random rotation.

But if the ring gets a random rotation of eg:90 then when the rocket passes through the ring goes to another direction and the next ring that is generated is on another direction.how to should i find the rotation of previous ring and generate a rotation so that the rocket can see the ring or is there any other way to do this?

To find a spot 50 in front of you, use `Vector v = transform.position + transform.forward*50;`.

But it seems more pen&paper work might be needed for this. Should the new ring maybe be kind of in front of them, and kind of facing them? Random 0-360 spin means the new ring can be sideways to you? Does it matter which way they go through? (since the new ring will be behind them if they go through backwards.) Wouldn’t the game be better if they could always see two rings ahead, to plan?

I’m assuming this is a 3D game. In visualizing your game, I think what you want to do is Instantiate() your ring somewhere in front of the rocket where the ring can be seen regardless of the direction the rocket is facing. Try this:

``````	public GameObject ring;

float counter = 50f;
float maxOffset = 10f;
float maxRotation = 75f;

void OnTriggerExit(Collider other)
{
Vector3 forward = GameObject.FindWithTag ("Rocket").transform.forward;
Vector3 pos = transform.position + forward * counter;
Quaternion q = Quaternion.LookRotation(forward);
GameObject clone = Instantiate(ring, pos, q) as GameObject;
clone.transform.Translate (Random.Range (-maxOffset, maxOffset), Random.Range (-maxOffset, maxOffset), 0F);
clone.transform.Rotate (Random.Range (-maxRotation, maxRotation), Random.Range (-maxRotation, maxRotation), 0F);
Destroy(gameObject);
}
``````

Note, I’m assuming your ring is on a Quad. If it is a physical object, then change the rotation line to use negative forward:

``````Quaternion q = Quaternion.LookRotation(-forward);
``````

This code works by first Instantiating the ring directly in front of the ship with a rotation that directly faces the ship. Then after it is created, the ring is moved on it local coordinates to offset the ring, and rotated on it local coordinates. You will have to play with ‘maxOffset’ and ‘maxRotation’ to see what is right for your game.