Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by inglipX · Jun 26, 2013 at 10:44 PM · javascriptvariablecalculation

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;
                 }
             }
 
 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BlueRaja_2014 · Jun 26, 2013 at 10:53 PM 0
Share

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.

avatar image inglipX · Jun 27, 2013 at 12:01 AM 0
Share

(1/100032)(temperature) / 2. ???

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image inglipX · Jun 26, 2013 at 11:11 PM 0
Share

what would the modifier be though?

avatar image Styxxx · Jun 27, 2013 at 07:34 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges