How to collide obstacles each other like flappy crush game in unity?
i try to collide obstacle each other each other on y axis like flappy crush game so any how can do?this is reference link:https://www.youtube.com/watch?v=4JLjOO6tlDE
Answer by Blue-Cut · Apr 16, 2016 at 09:55 AM
------------- About collision ----------------
Check the documentation : http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
Your 2D birds and obstacle need to have a 2D collider on their sprite. Put on these elements a custom script that implements OnTriggerEnter2D
. This method will be called when an other collider enter in their area.
You could for example put it on the obstacle and do something like this :
// We are in the script of the obstacle
void OnTriggerEnter2D(Collider2D other)
`{
// "other" is the bird that collides with it , you kill him
Destroy(other.gameObject);
}
------------- About moving obstacles ----------------
You have 1 pipe up and 1 pipe down. And at each frame, you make them move until they collide.
// Set this to 1 or -1 to make your pipe move up or down
public int direction;
// The speed of the pipe
public float speed;
void Update()
{
// use the Translate method to move a transform
transform.Translate(0f, speed*direction*Time.deltaTime, 0f);
}
// Then check the collision between the obstacles
void OnTriggerEnter2D(Collider2D other)
`{
// Let's assume that you identify your pipe with a tag
if(other.tag == "Pipe")
{
// Inverse the direction so the pipes get back to their positions
direction *= -1;
}
}
Complete this with something similar to inverse the position again when the pipes are back to their initial positions. Notice that your 2 pipes always have an opposed direction at the same time.
I wrote this in blind but this is the idea.
I guess that is where your problem starts. The birds are involved here.
What you need to do is setting some boolean true when the obstacles collide and false when they stop colliding.
Then next to this, you check if your bird collide with an obstacle. If it collides with an obstacle AND your boolean is true because the obstacles are colliding, then your bird is killed.
I don't crush bird actually what i need i explain: in my game two obstacles (pipe)on y axis now obstacles are fix position buy now i want to this two obstacles collide with each other on y axis.
Edit, is this what you were thinking about ?
What I am giving you here is a kind of guideline, copying this code won't be enough if you want that all work as you want. But it's definitly a good base to start.