- Home /
Have on variable affect another one ? Calculations help!?
I have script which controls air temperature : var temperature : float = 75f ;
and (human) body temperature : var bodytemperature : float = 98.6f ;
. As the player walks/runs/jumps his body temperature increases by a small decimal : var TempRateMovement : float = .0003;
. I have been trying to make my temperature variable affect the TempRateMovement. As the the (air) temperature increases, i want the TempRateMovement to increase, and as it decreases the rate also will decrease. How can i calculate this?? Thank you! Here is the TemperatureController.js:
#pragma strict
var temperature : float = 75f ; //or any other value;
var bodytemperature : float = 98.6f ; //or any other value;
var source : AudioSource;
var rectHeight: float;
var rectLength: float;
var style: GUIStyle;
var styletext : GUIStyle;
var humanstyle : GUIStyle;
var cloudstyle : GUIStyle;
var normalSymbol : Texture2D;
var moving : boolean = false;
var sprinting : boolean = false;
var idle : boolean = false;
var temperatureSymbol : Texture2D;
var humanTex : Texture2D;
var cloudTex : Texture2D;
var hotSymbol : Texture2D;
var coldSymbol : Texture2D;
var SomewhatCold : Texture2D;
var SomewhatCold2 : Texture2D;
var SomewhatHot : Texture2D;
var SomewhatHot2 : Texture2D;
var TempRateMovement : float = .0003;
var TempRateIdle : float = .00025;
var player : FPSRigidBodyWalker;
var headbob : Ironsights;
function Update (){
TempAffectsOnPlayer();
styletext.normal.textColor = Color.Lerp(Color(0, 0.83137254902,1,255), Color(1,0.21568627451,0.21568627451,255), temperature/98.6);
if (temperature >= 101){
temperatureSymbol = hotSymbol;
}
else
{
if (temperature > 60 && temperature <85){
temperatureSymbol = normalSymbol;
}
}
if (temperature <= 10){
temperatureSymbol = coldSymbol;
}
if (temperature <= 60 && temperature > 32){
temperatureSymbol = SomewhatCold;
}
if (temperature <= 32 && temperature > 10){
temperatureSymbol = SomewhatCold2;
}
if (temperature >= 85 && temperature < 95){
temperatureSymbol = SomewhatHot;
}
if (temperature >= 95 && temperature < 101){
temperatureSymbol = SomewhatHot2;
}
if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
{
moving = true;
idle = false;
bodytemperature += TempRateMovement;
}
else
{
moving = false;
idle = true;
TempRateMovement = .00003;
if (bodytemperature > 98.6)
bodytemperature -= TempRateIdle;
}
if (moving && Input.GetKey("left shift") && !idle)
{
sprinting = true;
TempRateMovement = .00005;
}
else
{
sprinting = false;
}
if (Input.GetKeyDown("space")){
JumpTempIncrease();
}
}
function OnGUI () {
var actualTemp = temperature-.05;
var actualbodyTemp = bodytemperature-.05;
GUI.Box (Rect(Screen.width - rectLength,Screen.height-rectHeight,200,80), temperatureSymbol, style);
GUI.Box (Rect(Screen.width+20 - rectLength,Screen.height-rectHeight,200,80), cloudTex, cloudstyle);
GUI.Label (Rect(Screen.width+1 - rectLength,Screen.height-rectHeight+60,200,80), actualTemp.ToString("f1") + "°F", styletext);
GUI.Box (Rect(Screen.width - rectLength,Screen.height-rectHeight-100,200,80), temperatureSymbol, style);
GUI.Box (Rect(Screen.width+20 - rectLength,Screen.height-rectHeight-100,200,80), humanTex, humanstyle);
GUI.Label (Rect(Screen.width+1 - rectLength,Screen.height-rectHeight-40,200,80), actualbodyTemp.ToString("f1") + "°F", styletext);
}
function JumpTempIncrease (){
yield WaitForSeconds (1.0);
bodytemperature += TempRateMovement + .00002;
}
function TempAffectsOnPlayer ()
{
//Movement && Headbob
if (temperature > 101 )
{
//everything greater than 101
player.walkSpeed = 1.8;
player.jumpSpeed = 0;
headbob.WalkHorizontalBobAmount = 0.25;
headbob.WalkBobPitchAmount = 0.065;
headbob.WalkBobSpeed = 4;
headbob.WalkBobRollAmount = 0.1;
player.sprintActive = false;
}
if (temperature < 101 && temperature >95)
{
// between 101-95
player.sprintActive = false;
headbob.WalkHorizontalBobAmount = 0.2;
headbob.WalkBobPitchAmount = 0.04;
headbob.WalkBobSpeed = 9;
headbob.WalkBobRollAmount = 0.07;
player.walkSpeed = 4;
player.jumpSpeed = 0.6;
}
if (temperature < 95 && temperature > 85)
{
//between 95-85
player.sprintActive = true;
headbob.WalkHorizontalBobAmount = 0.11;
headbob.WalkBobPitchAmount = 0.0175;
headbob.WalkBobSpeed = 12;
headbob.WalkBobRollAmount = 0.01;
player.jumpSpeed = 1.3;
}
if (temperature < 85 && temperature > 75)
{
// between 85-75
player.runSpeed = 7.8;
headbob.WalkHorizontalBobAmount = 0.14;
headbob.WalkBobPitchAmount = 0.02;
headbob.WalkBobSpeed = 11;
headbob.WalkBobRollAmount = 0.03;
player.jumpSpeed = 2.6;
}
else
{
if (temperature < 75){
player.runSpeed = 9;
headbob.WalkHorizontalBobAmount = 0.11;
headbob.WalkBobPitchAmount = 0.0175;
headbob.WalkBobSpeed = 12;
headbob.WalkBobRollAmount = 0.01;
player.jumpSpeed = 3;
}
}
}
You could calculate the difference in air temperature each frame, and use that to calculate the difference of TempRate$$anonymous$$ovement
. Or, you could just make TempRate$$anonymous$$ovement
some small percentage of the air temperature.
Answer by Styxxx · Jun 26, 2013 at 10:56 PM
Sounds like what you want is a property rather than a member variable, a readonly property would give you a simple way of calculating the total value automatically every time you call TempRateMovement. e.g.
private float BaseAmount;
private float Modifier;
public float TempRateMovement
{
get { return BaseAmount+Modifier; }
}
(I'm sorry but I don't speak Java)
Whatever you want. It was just an example of a formula producing the property value. Reading your question, I'd suggest you use something like; return (Temperature-75)*0.001
That would give you a movement of 0.001 per degree over 75.
Answer by Tarlius · Jun 27, 2013 at 08:36 AM
I think you could make your code a lot easier to read if you separate the logic for changing the temperature from the UI. It seems that you are actually using the previous frame's temp to choose the icon to display for your UI, for example. Anyway, if you simplify your update, you can write your logic quite easily. Since you didn't specify exactly how you want the TempRateMovement to be effected by air temperature, its impossible to give exactly what you want, but this should get you started.
public float currentTemp = 98; // body temp
public float environmentTemp = 75; // air temp
void UpdateTemperature() {
const comfortableEnvironmentTemp = 75; // Note: I don't speak Fahrenheit
const normalBodyTemp = 98; // Note: I'm not a doctor;
float environmentEffect = (environmentTemp - comfortableEnvironmentTemp) * 0.1;
float naturalCooling = (currentTemp - normalBodyTemp) * 0.1;
// Make the player heat up more if moving
float movementHeating = 0;
if(sprinting) movementHeating += 1;
if(moving) movementHeating += 0.5;
float variance = environmentEffect + naturalCooling + movementHeating;
variance *= Time.deltaTime; // Don't use tiny values, use deltaTime to apply a "per second" effect
currentTemp += variance;
}
void Update() {
CheckControls(); // Check the controls, apply running/etc
UpdateTemp(); // Updates the temp based on the input
TempEffectsOnPlayer(); // Apply the effects of the new temperature on the player
UpdateUI(); // Pick the icon to display in the UI based on the new temperature
}
I've tried to split up each factor you specified into its own "stage" which you can then play with to get your desired result. I don't claim what I've written will be close (or even physically plausible), but hopefully it will give you some direction. As it is, its basically:
(10% of the difference between comfortable temp and air temp + 10% of the difference between normal body temp and current body temp + 1 degree if running) per second
By the way- you should might want to consider turning the running/sprint/idle state into an enum. Unless there are times where you can be running and sprinting or idle and running/etc, it will make it much easier to manage.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104937im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Variable Not Changing. 0 Answers
InputField in a different scene to Text 1 Answer
Changing Variable OnMouseClick? Help! 1 Answer
Change variable value of a script from another script. both in different Game object 1 Answer
Trivial question for Javascript/Unityscript experts: static variables? 4 Answers