Question by
beginnerBume2ya · Aug 14, 2017 at 07:10 AM ·
unity 5rotationangle
[Unity C#] I want to control the rotation.
I am creating an air speedometer. I would like to receive the speed value from the external script and control the rotation with that value. But there is a problem. This is because the rotation values are different within the scale displayed on the speedometer and within Unity. 40 kn speed 40 If you need to stop on the grid, inside Unity inside this angle will not fit from 30. So I used Mathf.LerpAngle in search, but I do not have the desired result value. I want to match the scale based on the received value. I would be pleased if you could help. I am writing a translation with a Korean who can not speak English well. I am sorry in advance. Thank you very much. Here is my incorrect script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AirplaneController : MonoBehaviour
{
public GameObject ownShip;
public float kn;
public float km;
public float ms;
private Rigidbody rb;
public float sp;
private void Start()
{
rb = ownShip.GetComponent<Rigidbody>();
}
void Update ()
{
float translation = Input.GetAxis("Vertical") * sp;
rb.AddForce(ownShip.transform.forward * Time.deltaTime * translation * 3.5f);
ms = rb.velocity.magnitude;
kn = rb.velocity.magnitude * 1.943844f;
}
void OnGUI()
{
GUI.Box(new Rect(10, 10, 150, 90), "Measurements");
GUI.Label(new Rect(20, 40, 120, 20), "kn " + kn);
GUI.Label(new Rect(20, 60, 120, 20), "km/h " + km);
GUI.Label(new Rect(20, 80, 120, 20), "m/s " + ms);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Airspeed : MonoBehaviour {
public GameObject ownShip;
public GameObject PIN;
public Vector3 target40 = new Vector3(0f, 0f, 30f);
public Vector3 target60 = new Vector3(0f, 0f, 70f);
public Vector3 target80 = new Vector3(0f, 0f, 115f);
public Vector3 target100 = new Vector3(0f, 0f, 165f);
public Vector3 target120 = new Vector3(0f, 0f, 205f);
public Vector3 target140 = new Vector3(0f, 0f, 235f);
public Vector3 target160 = new Vector3(0f, 0f, 265f);
public Vector3 target180 = new Vector3(0f, 0f, 290f);
public Vector3 target200 = new Vector3(0f, 0f, 320f);
void Start ()
{
}
void Update ()
{
float velocityZ = ownShip.GetComponent<AirplaneController>().kn;
velocityZ = Mathf.Clamp(velocityZ, 0f, 200f);
if (velocityZ >= 0.1f && velocityZ <= 40)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target40.z, Time.deltaTime));
}
if (velocityZ >= 40 && velocityZ <= 60)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target60.z, Time.deltaTime));
}
if (velocityZ >= 60 && velocityZ <= 80)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target80.z, Time.deltaTime));
}
if (velocityZ >= 80 && velocityZ <= 100)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target100.z, Time.deltaTime));
}
if (velocityZ >= 100 && velocityZ <= 120)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target120.z, Time.deltaTime));
}
if (velocityZ >= 120 && velocityZ <= 140)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target140.z, Time.deltaTime));
}
if (velocityZ >= 140 && velocityZ <= 160)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target160.z, Time.deltaTime));
}
if (velocityZ >= 160 && velocityZ <= 180)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target180.z, Time.deltaTime));
}
if (velocityZ >= 180 && velocityZ <= 200)
{
PIN.transform.localEulerAngles = new Vector3(0f, 0f, Mathf.LerpAngle(PIN.transform.localEulerAngles.z, target200.z, Time.deltaTime));
}
}
}
Comment