- Home /
Code in OnCollisionEnter?
Attached is my game scene. The user can have maximum of 5 collisions with the obstacles. So I would like to know what must be the code when the ball hits the obstacle? I mean the code for continuing the game with ball moving forward(transform.translate).
Below code is currently present in OnCollisionEnter function in BallController program:
transform.position = new Vector3( transfomr.position.x, transform.position.y - 0.1f, transform.position.z)
transform.Translate (Time.deltaTime*2, 0, 0);
rest of the code is for other tasks like reducing the no of balls remaining etc.
This is working fine. But sometimes after collisions, the ball going in an entirely different direction & finally it exits the scene slowly. I don't know what is the reason behind it. Am I missing something? This is the first time I am developing a code in Unity.
you are moving object 0.1 downward when it collides. so if it collides with a box below it, it keeps calling OnCollisionEnter. I think that might be causing it. try debugging position when it collides and after you move object.
The gap between the obstacles are enough to prevent collision immediately after a collision. I tried playing the game after removing 0.1f. Sometimes ball is going somewhere slowly out of game scene after few collisions.
You should always have the ball ricochet towards the center of the level rather than a set down vector. That way it will always bounce away from other blocks rather than getting stuck in corners. Also, if the ball is moving slowly off screen after a certain number of collisions, check the debug console to make sure you're not getting any null reference exceptions. If you get them during game play, the script will crash but the game will continue, this means whatever the ball was doing last, it will continue doing. So if it crashes while moving a certain direction, it will likely continue on in that direction.
Conversely, it might be better to use a rigidbody in this instance ins$$anonymous$$d since it already has collision and ricochet built in. This way you could also apply a force to the ball should it start moving backwards in order to continue it on the correct path.
use on trigger enter...dont use collisions and when chances left is zero then stop its motion
Answer by felixpk · Oct 28, 2014 at 05:47 PM
The problem is that you are not checking whether the collision was fired because you hit a top or a bottom block. You could for example:
set the tag of the topBlocks to "topBlock"
set the tag of the bottomBlocks to "bottomBlock"
void OnTriggerEnter2D(Collider2D other) { if (other.tag == "topBlock") { transform.position = new Vector3( transform.position.x, transform.position.y - 0.1f, transform.position.z); // & reduce lifes and so on } else if (other.tag == "bottomBlock") { transform.position = new Vector3(transform.position.x, transform.position.y + 0.1f, transform.position.z); // & reduce lifes and so on } }
And the most important: post your code and post it formated right!