- Home /
Smooth movement for the block upon player collision,Smooth box movement
Question 1: Hello, I recently started working in Unity, I have no prior experience. And in my game I am trying to push a box with a bb8 model (player). After searching a lot I was able to push the box in the direction the player moves, however I tried searching but couldn't find anything relevant for smooth movements of the box. So as of now the box moves but it is jerking during the movement. Following is the code I used from one of the documentations.
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
{
return;
}
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
{
return;
}
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * pushPower;
}
following are the values for the attributes in the rigidbody component of the block Mass: 1 Drag: 0 Angular Drag: 0.5 User Gravity: Checked isKinematic: Unchecked interpolate: interpolate collision detection: Continuous Dynamic Freeze pos: checked y only freeze rotation: checked y and z
Question 2: Also After this, I'm basically planning on making the player move this block to a certain position and when the block reaches that position, I want some door or wall ( a cube at the moment) to open and when the player moves the block away from that position the door or the wall closes back. I was looking at some functions like onCollisionTrigger and stuff, but also if anyone has anything specific thye can suggest for this or help in any way, it would appreciated. Thank you