Rotating Gameobject not Colliding
When I rotate an object, the object rotates perfectly, but it just passes through anything it collides with.
function Update(){
transform.localEulerAngles.y += Input.GetAxis("Mouse X");
transform.localEulerAngles.x += Input.GetAxis("Mouse Y");
}
None of the colliders are marked as triggers, and there is nothing else going on. Just two objects next to each other, one rotating and one stationary, the rotating one passing through the stationary one. Any way to prevent this or fix it?
Answer by 5c4r3cr0w · May 31, 2016 at 05:21 AM
First make sure that you have attached rigid body to your game object which you want to rotate.
Second, Don't use transform function because it over writes values of physics engine. By using transform.localEulerAngles you're forcing game object to rotate despite of the colliders. Instead use RigidBody.angularVelocity to Rotate your game object so it will collide with every collider.
So your code would look something like :
void Update(){
float Xval = Input.GetAxis("Mouse Y");
float Yval = Input.GetAxis("Mouse X");
rb.angularVelocity += new Vector3(Xval , Yval, 0);
}
Check out this video for more understanding :
And to rephrase that 2nd paragraph: Unity allows you to move and spin things in an exact perfect way with no interference -- like moving things around yourself in Edit mode. In a script, changing transform.position and rotation does that.
Unity also allows you to "push" objects and let the move and bounce as if they were real -- use the physics system and all physics commands for that.
It works best if you do one of the other, and don't mix them.
What if I want to have the rigidbody that has "Is $$anonymous$$inematic" enabled? Will it still rotate and stop once it hits the other object?
If rigid body is set to $$anonymous$$inematic then any physics functions won't be applied to it. Force , velocity , angular velocity , joints, gravity , all of it won't affect it if is$$anonymous$$inematic is enabled. So it can not be rotated with angular velocity as @Owen Reynolds said you can use one of it and can not mix up both.