Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question how to switch speedometer speed after switching car

Discussion in 'Scripting' started by EpikBananaGames, Aug 9, 2022.

  1. EpikBananaGames

    EpikBananaGames

    Joined:
    Jan 16, 2022
    Posts:
    1
    hi guys so i have made a speedometer script and i have also made a car switching script to switch cars when the letter V is pressed. when the car is changed the speedometer speed is not changed, so can anyone help me change the speedometer speed to the next car?

    Speedometer script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3.  
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Speedometer : MonoBehaviour
    8. {
    9.     public Rigidbody target;
    10.  
    11.     public float maxSpeed = 0.0f; // The maximum speed of the target ** IN KM/H **
    12.  
    13.     public float minSpeedArrowAngle;
    14.     public float maxSpeedArrowAngle;
    15.  
    16.     [Header("UI")]
    17.     public Text speedLabel; // The label that displays the speed;
    18.     public RectTransform arrow; // The arrow in the speedometer
    19.  
    20.     private float speed = 0.0f;
    21.     private void Update()
    22.     {
    23.         // 3.6f to convert in kilometers
    24.         // ** The speed must be clamped by the car controller **
    25.         speed = target.velocity.magnitude * 3.6f;
    26.  
    27.         if (speedLabel != null)
    28.             speedLabel.text = ((int)speed) + " km/h";
    29.         if (arrow != null)
    30.             arrow.localEulerAngles =
    31.                 new Vector3(0, 0, Mathf.Lerp(minSpeedArrowAngle, maxSpeedArrowAngle, speed / maxSpeed));
    32.     }
    33. }
    Car switching script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CarChanger : MonoBehaviour {
    6.  
    7.     public GameObject mainCamera;
    8.     public GameObject tocusCamera;
    9.     public GameObject mainCar;
    10.     public GameObject tocus;
    11.  
    12.     void Update() {
    13.         if(Input.GetKeyDown(KeyCode.C)) {
    14.             mainCamera.SetActive(false);
    15.             tocusCamera.SetActive(true);
    16.             mainCar.SetActive(false);
    17.             tocus.SetActive(true);
    18.         }
    19.         if(Input.GetKeyDown(KeyCode.V)) {
    20.             mainCamera.SetActive(true);
    21.             tocusCamera.SetActive(false);
    22.             mainCar.SetActive(true);
    23.             tocus.SetActive(false);
    24.         }
    25.     }
    26. }
    27.  
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    You will have to assign the rigidbody of your main car to your speedometer target.
     
  3. doodler345

    doodler345

    Joined:
    Dec 30, 2021
    Posts:
    27
    It seems like there is no real connection between those two scripts right now.
    I think you put the rigidbody component of your MainCar into the Inspector for “public Rigidbody target”, right?
    So the script will only know the values from your MainCar.

    You could make two variables:
    Code (CSharp):
    1. public Rigidbody rbMainCar;
    2. public Rigidbody rbSecondCar;
    In the Inspector you can now store both cars.

    Next, in the Updatefunction of Speedometer:
    Code (CSharp):
    1. if(rbMainCar != null)
    2.   speed = rbMainCar.velocity.magnitude * 3.6f;
    3. else
    4.   speed = rbSecondCar.velocity.magnitude * 3.6f;
    This means when rbMainCar can not be found (because its SetActive to false) it will take the speed from the Second Car.
    So that way it will automatically choose the right speed.