Then its basically what I gave you but a different axis you rotate about. Also in Unity is a left hand rule system where positive X is to the right. positive Z is straight forward and positive Y is straight up.
If you spin around the Y axis you twirl in place like a ballerina
if you spin around the Z axis you twist about like doing a cartwheel.
if you spin about the X axis you will roll along the ground.
Now this only works to spin along the world X axis if we are facing directly forward along the world Z Axis.
Fortunately each object’s transform keeps track of its own personal Forward (z) , Up (y) and Right(x) axis
So to make sure we roll as we move we first need to Look at the thing we are rolling towards:
transform.Lookat(Goal);
Now that we are facing the right way we use this:
transform.Rotate(transform.right*moveSpeed*time.deltaTime);
Now to put it all together:
void Update () {
if (Input.GetMouseButtonDown(0))
{
player.transform.LookAt(Goal);
player.transform.position = Vector3.MoveTowards(player.transform.position, Goal, moveSpeed * Time.deltaTime);
player.transform.Rotate(player.transform.right*moveSpeed*Time.deltaTime);
}
}
Technically we only want to look at our Goal once. If we keep looking at it each time we’ll reset our roll to the start. So we need to catch the single frame they press the button down like this:
void Update () {
// this is true the one frame they click the button
if (input.GetMouseButton(0))
{
player.transform.LookAt(Goal);
}
// this is true as long as the button is held down
if (Input.GetMouseButtonDown(0))
{
player.transform.position = Vector3.MoveTowards(player.transform.position, Goal, moveSpeed * Time.deltaTime);
player.transform.Rotate(player.transform.right*moveSpeed*Time.deltaTime);
}
}
Finally, you might want to have all this happen with just one button click, not have the user constantly hold down the mouse button. That is what Coroutines are for:
void Update () {
// this is true the one frame they click the button
if (input.GetMouseButton(0))
{
player.transform.LookAt(Goal);
StartCoroutine(MoveCube());
}
}
IEnumerator MoveCube()
{
while (player.tranform.position != Goal)
{
player.transform.position = Vector3.MoveTowards(player.transform.position, Goal, moveSpeed * Time.deltaTime);
player.transform.Rotate(player.transform.right*moveSpeed*Time.deltaTime);
// this says yield control back to Unity until the
// next frame.. but pick up right here when we get
// called again
yield return null;
}
}