Using an object rotation to inform radial 360 fill.

Hi all,

I am super new to Unity C# scripting and scripting in general and would first like to thank the members of this community that have helped me get this far. The resources available have been extremely valuable in teaching myself to get this far. Now I have a stumbling block that seams to be a stretch too far from LinkedIn learning and YouTube.

I have an object that rotates to face the direction of the mouse cursor which works very well. The next step is as that object rotate it draws a circle around its self. As it reverses the circle is ‘undrawn’. So I have tried my best to find resources on it, admittedly I may have missed something. What I am looking to do is build a script that will look at the -180 to 180 rotation of the GameObject and translate that to the fill amount 0 to 1 of a Radial 360 fill of an image.

Can anyone point me in the right direction?

You can check the angle between two rotations using Quaternion.Angle(). If you just want the angle of your rotation, you check the angle between “your rotation” and “no rotation”, which is Quaternion.identity. So together, it should look like “Quaternion.Angle(Quaternion.identity, transform.rotation)”.

This will not tell you if you are rotated left or right, so you may also want to check the angle between “right” and your rotation by calling “Quaternion.Angle(Quaternion.Euler(Vector3.right), transform.rotation)”. (-90 to 90 means rotated right, anything else means rotated left)

This function will return an angle in degrees, but be careful when working with rotations, because Unity likes to sometimes use degrees and sometimes use radians. Just mouse-over a function and it will tell you degrees or radians.

Thanks a lot for this. I will have a play and see how I get on :slight_smile:

I am so very struggling with this.

This is my “monkey level” code im trying to use.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Logo_Website_UI
{

    public class RadialFillAmount : MonoBehaviour
    {

        public Image image;
        public GameObject obj;

        void Start()
        {
            image.fillAmount = 0f;

           
        }

        void Update()
        {

            image.fillAmount = obj(Quaternion.Angle(Quaternion.identity, transform.rotation));

        }
    }
}

Essentially I want to attach my game object to the Public GameObject, then have the rotation of that increase the fill amount. I apologise if I am missing something really obvious.

If it helps I need the Y rotation value to inform the image.fillAmount.

What I am looking to do is build a script that will look at the -180 to 180 rotation of the GameObject and translate that to the fill amount 0 to 1 of a Radial 360 fill of an image.

Perfect, you know exactly what you want! That really is half the battle.

Not quite sure what the obj method does, it doesnt seem to be defined. What you probably want is:

  • void Update()
  • {
    • image.fillAmount = (obj.transform.rotation.eulerAngles.z + 180f) / 360f;
    • }

Just select your object in the inspector and see that the Transform shows the rotation changing from -180 to 180. That shows the rotation in Euler Angles, so we need to get these with Quaternion.EulerAngles. We then have the value like you said in -180 to 180, and we need them in another range. By adding 180 we get the rotation from 0 to 360, and divide that by 360 to get it from 0 to 1.

Mate, I could kiss you on the mouth! With a little tweaking that worked perfectly thanks so much for your support. :slight_smile:

No problem at all, thank you so much for clearly understanding your problem and explaining it so well. That is an amazing skill to have as any developer and it should really help you a lot.