My 3d ball was moving continuously in the forward direction so I want to give X-rotation for this plus game player was dragging ball horizontally on the screen so for this horizontal movement, I want to give Z-rotation.
To achieve my desire, the result I have made ball parent and child game objects, where parent game object only contains collider and rigidbody and this will do the actual physical movement. Another side, child game object only contains mesh renderer, this will do desire rotation.
Following image gives you more idea about my structure:
Ball Parent:
Ball Child:
For giving rotation to ball mesh as like parent physics ball, I have written this code:
public class BallMeshRolling : MonoBehaviour
{
private Vector3 ballLastPosition;
void Start ()
{
ballLastPosition = transform.parent.position;
}
void Update ()
{
// implementation-1
// float speed = Vector3.Distance (transform.parent.position, ballLastPosition) * 30f;
// transform.RotateAround (transform.position, Vector3.right, speed);
// implementation-2
// Vector3 differenceInPosition = (transform.parent.position - ballLastPosition).normalized;
// transform.Rotate (differenceInPosition * 10f);
// implementation-3
Vector3 differenceInPosition = (transform.parent.position - ballLastPosition).normalized * 50f;
Quaternion rotation = Quaternion.LookRotation(differenceInPosition);
transform.rotation = rotation;
ballLastPosition = transform.parent.position;
}
}
But none of the above ways, working properly, so I expect some other better suggestions from your side.
EDIT:
Overall I am trying to achieve something like this:
Catch up - Ketchapp - Highscore 1274