C# Min/Max Rotation based on Float?
I am trying to work on a non GUI speedometer. I have my speed float set up and am trying to work out how to get the needle of the dial to rotate from a minimum angle to a maximum angle depending on the value of the speed float. Any help would be much appreciated!
using UnityEngine;
using System.Collections;
public class Speedos : MonoBehaviour {
//My speedometer needles.
public GameObject RPMSpeedo;
public GameObject MPHSpeedo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Speed float value.
GameObject sedan = GameObject.Find("Sedan");
RCCCarControllerV2 sedanScript = sedan.GetComponent<RCCCarControllerV2>();
sedanScript.speed -= 0f;
}
}
I can't even seem to find a way of rotating a separate gameobject using C#?
Answer by Mindmapfreak · Jun 23, 2016 at 07:06 PM
Something like this should work:
float angle = Mathf.Clamp((speed-MIN_SPEED)/MAX_SPEED*(MAX_ANGLE-MIN_ANGLE)+MIN_ANGLE, MIN_ANGLE,MAX_ANGLE);
NeedleObject.transform.localRotation = Quaternion.AngleAxis(angle, NeedleObject.transform.forward);
Maybe you have to use transform.up or transform.right instead of transform.forward, depending on the orientation of your gameobject.
Hmm could I get you to explain to me why each part of the code is there? I understand what its apart from the Quaternion part but I don't really understand why so I can't figure out what I need to change to get it to work. I've tried changing the transform direction like you said and have tried almost every single combination of $$anonymous$$ and max angles.
using UnityEngine;
using System.Collections;
public class Speedos : $$anonymous$$onoBehaviour {
public GameObject RP$$anonymous$$Speedo;
public GameObject $$anonymous$$$$anonymous$$HSpeedo;
private int maxangle$$anonymous$$$$anonymous$$H;
private int $$anonymous$$angle$$anonymous$$$$anonymous$$H;
public float angle;
// Use this for initialization
void Start () {
maxangle$$anonymous$$$$anonymous$$H = 210;
$$anonymous$$angle$$anonymous$$$$anonymous$$H = 0;
}
// Update is called once per frame
void Update () {
GameObject sedan = GameObject.Find("Sedan");
RCCCarControllerV2 sedanScript = sedan.GetComponent<RCCCarControllerV2>();
angle = $$anonymous$$athf.Clamp(sedanScript.speed / sedanScript.maxspeed * (maxangle$$anonymous$$$$anonymous$$H - $$anonymous$$angle$$anonymous$$$$anonymous$$H) + $$anonymous$$angle$$anonymous$$$$anonymous$$H, $$anonymous$$angle$$anonymous$$$$anonymous$$H, maxangle$$anonymous$$$$anonymous$$H);
$$anonymous$$$$anonymous$$HSpeedo.transform.localRotation = Quaternion.AngleAxis(angle, $$anonymous$$$$anonymous$$HSpeedo.transform.up);
}
}
I should say that it worked for a moment at the start but the gameobject was being made horizontal, I couldn't get the needle of the speedometer to start at 0 and when the speed of the car reached a certain speed the needle flipped out and span around the place.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083445im_/https://answers.unity.com/themes/thub/images/avi.jpg)