- Home /
After collision rotate and move
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.
Answer by roamcel · Sep 13, 2011 at 11:45 AM
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.
Oh the Y axis... I think I was working with Z all the time. I don't have anything working atm, but I worked with addForce. Do I have to apply a new force everything it collides? Or will that keep doing so after it made collision, if it is placed in a Update function? I will edit the main post to make my idea more clear. Thanks anyway!
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 ins$$anonymous$$d altering the TRANSFOR$$anonymous$$ (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.
I got it to work a few days ago! Thank you for helping me out :)
function Update() { transform.Translate(Vector3.forward * Time.deltaTime); }
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "$$anonymous$$ountainPrefab"){
rigidbody.transform.eulerAngles.y = Random.Range(20, 180);
rigidbody.transform.Translate(Vector3.forward * Time.deltaTime);
}
}<code>
Your answer
![](https://koobas.hobune.stream/wayback/20220612211931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move a rigid body in an area of a platform 0 Answers
How to roll a ball ? 2 Answers
Help with plat-former character rotate 1 Answer
movement doesnt stop when jump pad is tapped at the same time 0 Answers