- Home /
What is causing my object to spin around and act funky when it hits another collider?
I recently added a rigidbody to my gameobject so that I can recieve ontriggerenter/exit calls. Though if my new unit hits something else with a collider some odd behavior occurs.
http://i.imgur.com/KzMHR46.webm:
Whats causing this to happen? And how do I stop the object from moving in a seemingly unpredictable way if it hits another collider?
You might want to freeze rotation on x and y on the ship's rigidbody
Yep that did the trick. Though what actually caused this? If I want to use any sort of physics on the future what sort of issues will I be tackling to avoid this specific problem?
The problem is in your code, nothing we can help you with, without it.
I mean, even with all the code removed (components disabled) that controls movement it still moves that way. I only have 1 class that controls that units movement, with that component disabled there is no further code of $$anonymous$$e that can cause it to move. The only way I can get movement is through that collider. I want to know what goes on with colliders and rigidbodies that causes such sporadic rotation and movement. And if I need to take steps to control that interaction in the future.
I can stop the rotation by setting up a loop to set the rotation to 0. Though even after that it goes back to rotating the moment the loop is not being ran.
You should be using BoxCollider2D for sprites, not Sphere collider. That's why it's spinning in 3 dimensions.
Answer by Addyarb · Mar 26, 2015 at 11:17 PM
As stated in the comments to the original question, locking the axis will prevent this from happening. The reason your gameObject moves around when it gets hit is because "Rigidbody" is a component, that tells the object it is attached to, to simulate Physics when it interacts with another collider. If you don't want to simulate Physics, you can use other methods of detecting collision, or you can lock all of the axes.
However, it is recommended that if your object is moving and attempting to detect collision, that you have a Rigidbody component on it at all times. I believe this is because rigidbodies detect collision using FixedUpdate instead of Update, which is more exact and will give you a more accurate collision setup.
Thanks for the reply!
Lets say I want collisions for the sake of this comment. When such a collision occurs, what process would I need to go through to slow down or stop the rotation/position changes? It seems just setting the position/rotation to an arbitrary value after the collision has happened and is no longer happening does not stop the physics simulation from trying to move the unit around on it's own accord.
No problem! It can be a bit confusing at first, sorry you're having issues.
Assu$$anonymous$$g you'd like collisions, you can achieve that in a few ways. If you have a building or other static (not moving) object, you can just use two box colliders - one with a trigger enabled that's a bit bigger and one with a trigger disabled so that other objects can't pass through it. This would be enough to trigger a collision.
However, if you are trying to detect collisions on something like a projectile, you are going to want a much more precise and dynamic collision detection. A Rigidbody in this case would give you the ability to effectively detect dynamic (moving object) collision.
Rigidbody comes with some great methods like AddForce, UseGravity, Is$$anonymous$$inematic, etc. However, not everyone wants a game that uses physics they can't control in every situation. This is where you would just use something like Transform.Translate(Vector3.Forward * time.DeltaTime) ins$$anonymous$$d of using Rigidbody.AddForce(100);
While you can achieve some great things when using a Rigidbody, sometimes you just use it for the extra accuracy in collision detection. In this case, you will want to lock down all axis and set your Collision Detection to "Continuous" or "Continuous Dynamic".
$$anonymous$$eep in $$anonymous$$d though, that altering the Transform of the object via code will override the Rigidbody's axis lock system.
Your answer
Follow this Question
Related Questions
Character falls through floor any suggestions? 1 Answer
How to get movable/pushable cubes right? 0 Answers
How to speed up? 3 Answers
Having trouble turning a Transform movement into a Rigidbody force. Code included 1 Answer
Rigidbody Collision? 1 Answer