Hello everyone. I am trying to apply relative force to a rigidbody (cylinder) at its contact point with the ground. The direction of the force applied has to change according to the surface which the cylinder is touching. The pictures show how the direction of the force, applied from rigidbody’s contact point(s), should change. Orange sphere is a contact point, yellow arrow - force that should by applied to the cylinder by the script, blue triangle on a cylinder just shows that it is not rotating when clearing the obstacle.
Current working code:
function OnCollisionStay (collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
contacts = collision.contacts.Length;
if(Input.GetKey(KeyCode.W)){
var dir : Vector3;
dir = (transform.position - contact.point);
rigidbody.rigidbody.AddForceAtPosition(Vector3.Cross(transform.right, dir).normalized * (Power / collision.contacts.Length) * Time.deltaTime, contact.point);
}
}
}
In this code, made using the suggestion of Habitablaba, I have created a vector (dir) from the center of the cylinder towards the contact point, crossed it with transform.right to create a direction offset and normalized it. Variable “Power” is a float, it is divided by the number of contacts so that the total force does not increase when the rigidbody has more than one contact with the surface.