Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so I made this script that heats up when the player moves but I cant figure out a way for him to cool down when he has stopped moving. how should I fix this?

code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Temperature : MonoBehaviour
{
public Slider staminaSlider;
public int maxStamina;

public float minTemperature = 32;
public float temperature = 32;
public float maxTemperature = 105;

public float temperatureFactor = 30;
public bool overheating;

[Header("Temperature Settings")]
public float freezingTemp;
public float currentTemp;
public float normalTemp;
public float heatTemp;
public Text tempNumber;
public Image tempBG;


void UpdateTemp()
{
tempNumber.text = currentTemp.ToString("00.0");
}

void Update()
{
float tempChange = Time.deltaTime * temperatureFactor;
if (Input.GetKey(KeyCode.LeftShift) && !overheating)
{
temperature = Math.Min(maxTemperature, temperature + tempChange);
if (temperature == maxTemperature)
{
overheating = true;
}
}
else if (!Input.GetKey(KeyCode.W))
{
temperature = Math.Max(minTemperature, temperature - tempChange);
if (temperature == minTemperature)
{
overheating = false;
}
}

if (currentTemp <= freezingTemp)
{
tempBG.color = Color.blue;
UpdateTemp();
}

else if (currentTemp >= heatTemp - 0.1)
{
tempBG.color = Color.red;
UpdateTemp();
}

else
{
tempBG.color = Color.green;
UpdateTemp();
}

if (staminaSlider.value > 0)
{
currentTemp += Time.deltaTime / 5;
}

else
{
if (currentTemp >= normalTemp)
{
currentTemp -= Time.deltaTime / 10;
}
}
}
}

What I have tried:

asking help on forums but have had no clear answer
Posted
Updated 5-Mar-21 21:49pm
Comments
BillWoodruff 6-Mar-21 4:47am    
We can't read your mind: show the code that creates the Class.

How do you handle "state of motion:" not running, starting run, running, stopping, stopped ? Show the code for that.

Moving and Temperature should be properties of your Player class, so you can do something like:
C#
if (player.Moving == true)
{
    player.Temperature++;
}
else
{
    player.Temperature--;
}
 
Share this answer
 
v2
Comments
Michael Ciurleo 6-Mar-21 4:14am    
ok so I've added your code in but now it says "player does not exist in current context"

what should I do?
Richard MacCutchan 6-Mar-21 4:17am    
Get a good book on C# and study it.
OriginalGriff 6-Mar-21 4:19am    
But ... but ... I'z an ub3rcod3rz! I don't need to learn nuffink!
Michael Ciurleo 6-Mar-21 4:38am    
Christ you guys I’m only a beginner, I may not know everything but that doesn’t make me a dummy. When you fail a math question do cry about it? No! You ask for help, like I’m doing, so I’m sorry if I’m not a genius, I just need help
Richard MacCutchan 6-Mar-21 4:49am    
And we are trying to help you. The problem you have, is that like so many would be developers, you seem to believe there is a shortcut to gaining knowledge and skill. You will not learn how to develop computer applications or games by posting questions here. You need to get hold of a good book on the language you want to learn and spend some time (a lot of time) studying it.
If you wrote the code to heat up, then it should be obvious: it's exactly the same process but with subtraction instead of addition ...

To be honest, if you can't work that out for yourself having written working code to heat up, then you probably should reconsider how far you have got in your course, and whether you either need to start again from the beginning, or abandon the whole idea of learning to develop code. Things will only get harder from here, not easier!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900