- Home /
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!
Yeah, only thing is, I'm not sure what GUI to use and the variables either, I've never really handled this sort of situation before.
Answer by getyour411 · Nov 28, 2014 at 11:08 PM
OnTriggerEnter () {
yourTemp++;
}
OnTriggerExit() {
yourTemp--;
}
Search for any number of examples here that include a timer/ti$$anonymous$$g element and do some maths based on the relative warm-up/cool down you want;
Answer by Kiwasi · Nov 28, 2014 at 11:21 PM
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.
The temperature script that I am looking for goes like this. Start off at a certain degree, and -1 degree after every 20-30 seconds or something like that. When you get in a radius of a certain gameObject, rise up +1 every 10-15 seconds or so. Hopefully that helps you a little more with your thinking.
Lol, not my project, not my thinking. The model you describe is not accurate.
However I can $$anonymous$$dlessly write out a script that does that for you.
public class Temperature : $$anonymous$$onoBehaviour {
public float temperature {get; private set}
void Start (){
StartCoroutine(Cool());
}
void OnTriggerEnter (collider other){
StartCoroutine ("Heat");
}
void OnTriggerExit (collider other){
StopCoroutine ("Heat");
}
IEnumerator Cool (){
while (true){
yield return new WaitForSeconds(30);
temperature -= 1;
}
}
IEnumerator Heat (){
while (true){
yield return new WaitForSeconds(10);
temperature += 1;
}
}
}
Answer by dkaloger · Sep 11, 2020 at 04:29 PM
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;
}
What's with the weird Δ symbols, does that even compile?