I have been trying to make a spawning object move around, make collision, then rotate and move an other direction again. My world has a ground, which has some level difference at some points en walls surrounding it. I have a empty GameObject that spawns a ball. I want the ball to keep moving around and bounce of walls once it makes collision.
The spawning, collision and rotation work fine. But I somehow can’t keep the prefab, which is a simple ball in my case and a rigidbody, to keep moving around the surface. The walls have a Boxcollider.
Can someone please help me? Struggling with this problem for some weeks now and it’s driving me crazy. I have tried many many things and lost sight of it a bit.
Given the lack of information you’re supplying regarding ‘keep moving around the surface’, I suppose that your issue is related to a common difficulty, which is understanding the DIRECTION of movement.
Quite simply, I suspect that you might not be making the object turn the appropriate angle around the world y axis, once it hits an obstacle.
- access the collision procedure
- add a Debug.Log("I collided"); instruction to it
- add the following to the same procedure
Quaternion newrotation = Random.rotation();
transform.rotation = newrotation;
In the case that the object actually does something resembling a random new direction, post how you calculate the new direction after bounce.
In unity's world coordinates, rotation around Z makes the object 'drill' forward. Turning in world coordinates require Y rotations, in fact. Additionally, yes, you are possibly required to apply a new force upon collision since, in the case that you're using a rigidbody, the object loses momentum once it hits an obstacle. If you're instead altering the TRANSFORM (which you should not do if the object has a rigidbody), then yes you should change the object direction in the Update function, rather than OnCollision.
In unity's world coordinates, rotation around Z makes the object 'drill' forward. Turning in world coordinates require Y rotations, in fact. Additionally, yes, you are possibly required to apply a new force upon collision since, in the case that you're using a rigidbody, the object loses momentum once it hits an obstacle. If you're instead altering the TRANSFORM (which you should not do if the object has a rigidbody), then yes you should change the object direction in the Update function, rather than OnCollision.
– roamcel