Canvas spinning on rolling object

I’m making a minigolf game (basically an advanced version of the rolling ball tutorial) and I’m trying to make a loading animation on the ball to indicate the force applied.

I use the following setup in the editor, with a canvas in worldspace attached to my ball prefab : alt text

I also have the following script attached, to ignore the rotation of the parent in the canvas :

using UnityEngine;

public class UISliderRotation : MonoBehaviour
{
    // Manipulate canvas rotation for slider elements on the ball so that it doesnt swing around
    private Quaternion m_CanvasRotation;

    private void Start()
    {
        // Local rotation at init
        m_CanvasRotation = transform.localRotation;
    }

    public void SetRotation(Quaternion newRotation)
    {
        m_CanvasRotation = newRotation;
    }

    private void Update()
    {
        transform.rotation = m_CanvasRotation;
    }
}

But when the ball is rolling, the canvas is spinning around the ball in a circular motion : alt text

Is it expected behavior and how can I have the canvas stay in place while the ball is rolling? Should I use a canvas in screen space with WorldToScreenPoint instead ? I’m looking for something that matches what is in the first pic.

Thanks in advance.

Hey,

If your canvas is in world space, you can use the world space coordinates. This means that you can simply set the position of the canvas equal to that of the player (with an offset if you want).

In code this would look like this:

public class FollowPlayer : MonoBehaviour
{
    public Transform Player;
    private Vector3 offset;

    private void Start()
    {
        offset = transform.position - Player.position;
    }

    private void Update()
    {
        transform.position = Player.position + offset;
    }
}