- Home /
How would I move an Object after collision?
I have my game set up in 2D mode, There is a side scrolling camera with a bar that follows it, during the game bricks will move across the screen, when they disappear off screen I am trying to get them to collide with the bar following and the move back by an x value of Random.Range(-6,-8) I am unaware if my code is wrong, or if I have an error, but I will post what code I have used.
function OnCollisionEnter(collision:Collision){
collision.transform.position = Vector3(Random.Range(-6,-8), Random.Range(-3,6),0);
}
Answer by Noob_Vulcan · Aug 04, 2014 at 04:41 AM
If your bar collider is moving right then Just Make Sure that
Your bricks/Bar has RigidBody. Because Collision is detected by Physics. So you will have to make one of them rigidbody.
If you have Followed Step 1 and still your code is not working then you have problem in bar position. Just make the Bar child of camera.
I hope this should resolve your problem.
as I was thinking about it more, is that going to always move the bricks to that set random range, or will it continue pushing them back, my goal is to make them get pushed back every time they are collided with.
can u tell me what exactly u want to achieve so that i can help u.
I figured it out, in case anyone else needs the code, here it is
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "WallL")
{
transform.position = Vector3(transform.position.x - Random.Range(-16,-20), Random.Range(-3,6),0);
}