0
\$\begingroup\$

I would like to ask a question about the scaling of objects to the size of the screen.

But the question is not a problem that can be solved by setting the "canvas scaler" to "scale with screen size."

It's about how to program the code.

As shown in the picture, the function I want to achieve is to make the hexagonal graphics change position with the player's operating angle.

And my code has successfully achieved this effect! However, I ran into a problem because the value of "rotationRadius" did not change and remained fixed at the previously set 320 f.

So when the screen gets smaller, the hexagonal pointer will move away from the ruler.

When the screen is enlarged, the hexagonal pointer will be in the arc ruler.

I want the "Hexagon Indicator" to move closely to the arc ruler. This hexagon must not deviate from the arc ruler under different screen sizes!

But I don't know how to do that.

Perhaps I can multiply the 320 f I set by some of the scale parameters.

But that's the problem. What is the calculation formula of this parameter?

Of course, in addition to multiplying the parameters, there should be other more appropriate methods, such as using anchor , but I really don't know how to do?

Could you please assist me in rewriting the following program code?


I drawing the green yellow and red meter graphic by the code below. And th e picture show the process what i do in unity.

![enter image description here

And the "angle" image is which I draw in AI(illustrator) then I put into unity Asset.

enter image description here

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

public class Exercise_Angle : MonoBehaviour
{
    [SerializeField]
    Transform rotationCenter;

    [SerializeField]
    public float rotationRadius = 320f;
    [SerializeField]
    private float posX, posY, angle = 0f;

    [SerializeField]
    private Image FillByHoldingAngle_Red;

    [SerializeField]
    private Image FillByHoldingAngle_Yellow;
    [SerializeField]
    float Max_Red_Angle, Max_Yellow_Angle, Max_Green_Angle,current_Angle;

    
    public int Exercise_type;// flexion:1, abduction:2, extension:3, external rotation: 4, internal rotation: 5
    public TMP_Text Angle;

    public float targetWidth;

    /*void Awake()
    {
        targetWidth = (float)Screen.width;
        float factor = Screen.width / 800;
        rotationRadius = 320f * factor;
    }
    */
        void Start()
    {

        if (Exercise_type == 1) //flexion:1
        {
            Max_Green_Angle = 180;
            Max_Yellow_Angle = Daterecord.flexion_SettingAngle;
            Max_Red_Angle = Daterecord.flexion_SettingAngle - 15;
        }
        if (Exercise_type == 2) //abduction:2
        {
            Max_Green_Angle = 180;
            Max_Yellow_Angle = Daterecord.abduction_SettingAngle;
            Max_Red_Angle = Daterecord.abduction_SettingAngle - 15;
        }
        if (Exercise_type == 3) //extension:3
        {
            Max_Green_Angle = 60;
            Max_Yellow_Angle = Daterecord.extension_SettingAngle;
            Max_Red_Angle = Daterecord.extension_SettingAngle - 15;
        }
        if (Exercise_type == 4) //external rotation: 4
        {
            Max_Green_Angle = 90;
            Max_Yellow_Angle = Daterecord.external_rotation_SettingAngle;
            Max_Red_Angle = Daterecord.external_rotation_SettingAngle - 15;
        }
        if (Exercise_type == 5) //internal rotation: 5
        {
            Max_Green_Angle = 90;
            Max_Yellow_Angle = Daterecord.internal_rotation_SettingAngle;
            Max_Red_Angle = Daterecord.internal_rotation_SettingAngle - 15;
        }
        else //internal rotation: 5
        {
            Max_Green_Angle = 180;
            Max_Yellow_Angle = 100;
            Max_Red_Angle = Max_Yellow_Angle - 20;
        }
            
    }

    // Update is called once per frame
    void Update()
    {
        FillByHoldingAngle_Red.fillAmount = Max_Red_Angle / Max_Green_Angle;
        FillByHoldingAngle_Yellow.fillAmount = Max_Yellow_Angle / Max_Green_Angle;
        Angle.text = Max_Yellow_Angle+ "°";
        //////////////////////////////////////////////////////////////////////////////////////////////
        posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
        posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius/2 ;
        transform.position = new Vector3(posX, posY, 0);
        //angle = angle + 0;
        //angle = angle + Time.deltaTime;
        angle = 3.14f+((current_Angle/ Max_Green_Angle) * 3.14f);

        if (angle >= 6.28f)
           angle = 3.14f;

    }


}
`

enter image description here enter image description here enter image description here

\$\endgroup\$
7
  • \$\begingroup\$ How are you drawing the green yellow and red meter graphic at the moment? We need to understand its scaling behaviour before we can help you fix it. Is the hexagon a child of a UI canvas? Try walking us through the steps needed to recreate this scene in a new, empty project. Once we can reproduce the problem, we can test potential solutions to make sure they'll work for you. \$\endgroup\$ Commented Dec 8, 2022 at 20:24
  • \$\begingroup\$ @DMGregory Thank you for your reply. I have attached my unity project in this link: drive.google.com/file/d/1RWeYFOGqm0S2vEaCtxzcrHpQJ_mPRk4U/… \$\endgroup\$ Commented Dec 10, 2022 at 14:27
  • \$\begingroup\$ Users here will not download and run an untrusted project. What I asked you to do was to edit the question to describe the steps to reproduce this layout. \$\endgroup\$ Commented Dec 10, 2022 at 14:36
  • \$\begingroup\$ @DMGregory Because I am making a VR Game. So I should set the Canva Render Mode : Sceen Space - Camera. But when I set the Canva Render Mode to Sceen Space - Camera, the hexagon is more far away from the arc ruler. \$\endgroup\$ Commented Dec 10, 2022 at 14:43
  • \$\begingroup\$ @DMGregory Thanks. I have already edited the QA. \$\endgroup\$ Commented Dec 10, 2022 at 15:02

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.