Rotating to match surface normal (curved surface).
After the normal reaches too high a value, this stops working:
(I've messed around with the speed values in the code below to see if that helps but to no avail).
RaycastHit hitInfo;
do
{
if (Physics.Raycast(transform.position, -transform.up, out hitInfo, 0.25f))
{
Quaternion targetRot = Quaternion.FromToRotation(transform.up, hitInfo.normal);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 5f);
Vector3 targetPos = hitInfo.point + FLOOR_OFFSET * hitInfo.normal;
if (_moveLeft)
targetPos -= transform.right;
if (_moveRight)
targetPos += transform.right;
transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * 0.1f);
Debug.DrawRay(transform.position, transform.position - transform.up * 1f);
}
yield return null;
}
while (_playing);
Around the point of the picture below, the object just gets closer to the surface, and stops rotating to match the normal. Even the ray doesn't seem to be coming out of -transform.up.
Any advice appreciated. I want the sphere to maintain a certain distance from the surface, and rotate according to its normal.
Your answer
Follow this Question
Related Questions
Creating points for robot (End Effector) to go through them 0 Answers
Rotate around a point with a specific angle 0 Answers
Can you make the player do a 180° / u-turn when a button is pressed? 0 Answers
Car weirdly flips 180 degrees while rotating to ground normal 0 Answers
Rotation (Quaternion.Lerp) not working 0 Answers