# Temperature Script?

Anyone have a basic script outline of a temperature meter (a GUI starting with 98 degrees and steadily decreasing or increasing), and can change temperature when in range of a specific object (i.e: a campfire for warmth)?

If someone could outline a script that starts me off, that’d be absolutely great. Thanks!

OnTriggerEnter () {
yourTemp++;
}

``````OnTriggerExit() {
yourTemp--;
}
``````

Temperature will move closer to the surrounding temperature over time. For a rather crude approximation I would do the following.

• Create a script that records the temperature of each GameObject
• Use a sphere cast at some defined frequency, or use OnTriggerStay, to get the temperature of all nearby GameObjects
• Adjust the temperature closer to the average of all the temperatures of nearby objects
• You could also define an ambient temperature to use if no GameObjects are close

For an accurate approximation grab a university level physics or engineering textbook, and look up the chapter on thermodynamics and heat transfer. This is not a light topic, I’d suggest running with an approximation rather then trying to create accurate temperature physics.

i know this is a very old question but i think this code snipet works perfectly

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class temp : MonoBehaviour
{
public float sc1 =3f; //specific heat capacity
public float sc2 = 6f;
public float sm1 = 100f; //mass
public float sm2 =200f;
public float sk1=0.609f;//insulation
public float sk2 = 0.58f;
public float tickspeed = 0.25f;
public float surfacearea = 25;
float C1; // heat capacity
float C2;

public float T1 = 370f; // temperature in kelvin
public float T2 = 315f;
float ΔQ;
float Q1;
float Q2;// thermal energy
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void FixedUpdate()
{

C1 = sc1 * sm1;
C2 = sc2 * sm2;
ΔQ = tickspeed * Mathf.Min(sk1, sk2) * (T1 - T2) * surfacearea * surfacearea;
T1 = T1 - ΔQ / C1;
T2 = T2 + ΔQ / C2;
Q1 = C1 * T1;
Q2 = C2 * T2;
}
``````