- Home /
Obstacles for kinematic rigidbody
Hello.
I have a GameObject that has kinematic rigidbody, and its movement is being handled by MovePosition
method.
I use kinematic rigidbody because in future this GameObject will have a lot of animations and does not require physics.
However, I want add obstacles into my scene and they are just some 3D models with colliders. Because my GameObject is kinematic, it will just pass through them.
Is there any methods to imitate obstacles that prevents kinematic GameObject pass through them?
private float GetNewDirectionRatio (Vector3 newDirection)
{
float angle = Vector3.Angle (newDirection, lastMovementDirection);
float ratio = 1.0f - angle / 180.0f;
return ratio;
}
private void RotatePlayer(Vector3 newDirection)
{
Vector3 newRotation = Vector3.Lerp (body.transform.forward, newDirection, turnRate * Time.deltaTime);
body.transform.rotation = Quaternion.LookRotation(newRotation);
}
protected void NewMovement(Vector3 movementDirection)
{
// Calculating speed according to the new direction
movementDirection.Normalize ();
currentSpeed = currentSpeed * GetNewDirectionRatio (movementDirection) + acceleration;
currentSpeed = Mathf.Min (currentSpeed, maxMovementSpeed);
// Applying new movement
movementDirection *= currentSpeed * Time.deltaTime;
playersRoot.MovePosition(transform.position + movementDirection);
lastMovementDirection = movementDirection;
RotatePlayer (movementDirection);
}
The method NewMovement
will be called inside update depending on Input value.
Is it possible to imitate collision?
Answer by Bunny83 · Nov 04, 2017 at 12:19 PM
No, a kinematic rigidbody does not perform any collision detection. Only non-kinematic rigidbody can detect collisions. Kinematic rigidbodies are able to wake up sleeping non-kinematic rigidbodies.
If you want to have full control over the movement of your player but have it ignore external forces you should use the CharacterController instead. It can still detect collisions with other colliders when it's moving. The CharacterController uses it's own simple raycast based collision detection. From the physics system point of view the character controllis is just like a static capsule collider. You "move" your character controller by using it's Move method
If for some reason you don't want to use the charactercontroller, if you want to have collisions you can't use a kinematic rigidbody.
Answer by exp626stitch · Dec 16, 2020 at 06:40 PM
@Bunny83 @SuperMasterBlasterLaser Acutally you can use raycasts:
Vector3 lastPos;
public void Start() {
lastPos = transform.position;
}
public void FixedUpdate()
{
lastPos = transform.position;
RaycastHit[] hits = Physics.RaycastAll(new Ray(lastPos, (transform.position - lastPos).normalized), (transform.position - lastPos).magnitude);
//Do something with it using hit
transform.position = hit[1].point;
}
If a collision dosnt occur in a visible frame, it didnt happen, so the raycast goes between where you were and where you are, and if there is a collision, then it teleports the object to the collision point, although I would recommend adding a offset based on half your size.