I have a variable foo = 0 of type float, if I really press the key (E), then the variable increases by 5.0f, if I press the key (Q), then the variable decreases by 5.0f, the question is if the variable foo reaches the number 30, then it can no longer increase, but can only decrease, it can increase only when it does not exceed 30, I can not write it down well in the code, have any ideas?
Hello.
this is a basic structure/scripting problem.YOu MUST be able to do it by your own, or will be asking things every time. If do not know how to use IF, ELSE IF, FOREACH and this things, you will never advance.
Go watch tutorials, 1 by 1, no worries, you have time.
It’s not really an answer to the question, but i do agree 100% with @tormentoarmagedoom . These are the basics of coding. Creating an if else statement or comparing values is coding chapter 1 lesson 1. And as mentioned, if you don’t know how to do that, start with learning it first. There are some great websites that can teach you the basics of coding, like codeschool and codeacademy. Also unity has some great tutorials for beginners that teach you some coding and how to use the unity engine at the same time. That said, i did want to provide you with a way to find out how to code this, what the basic process is and what the end result might look like.
using UnityEngine;
public class KeyAdd : MonoBehaviour
{
//First we write some pseudo code:
/*
if E is pressed:
if the current total plus the e key value is smaller or equal to 30:
add the e key value to the total.
if Q is pressed:
if the current total minus the q key value is greater or equal to 0:
subtract the q key value from the total.
*/
//From this pseudo code we see that we need to create some variables:
public int total = 0;
public int eKeyValue = 5;
public int qKeyValue = 5;
//We translate it to actual code:
public void Update()
{
//if E is pressed:
if (Input.GetKeyDown("e"))
{
//if the current total plus the e key value is smaller or equal to 30:
if (total + eKeyValue <= 30)
{
//add the e key value to the total.
total += eKeyValue;
}
}
//if Q is pressed:
if (Input.GetKeyDown("q"))
{
//if the current total minus the q key value is greater or equal to 0:
if (total - qKeyValue >= 0)
{
//subtract the q key value from the total.
total -= qKeyValue;
}
}
}
}