- Home /
How create ONLY a object ?!
Hey guys. i am trying build chekpoints for my game . everytime player getkeydown for 2 s , game create only a chekpoint (if his mana was enough ) ,but when i try create my chekpoint object ,with instantiate() ,unity build chekpoints over and over untill my codintion will be wrong(mana>= 20 only work ,i think process is so fast for this cpenough condintion will ignore). my code is this .(ac is a paramtere for knowing distance between chekpoints, player.gd is player on ground) let me know if u need more information . tnqs for your time :)
void update()
{
ac = player.transform.position.x - levelManager.currentChekPoint.transform.position.x;
if (ac > 3 || ac < -3)
cpenough = false;
else cpenough = true;
//making chekpoint(cp)
if (player.gd)
{
if (Input.GetKeyDown(KeyCode.E))
{ mcpcounter = mcp;
}
if (Input.GetKey(KeyCode.E))
{
if (manamanager.mana >= 20 )
{
if (cpenough == false)
{
mcpcounter -= Time.deltaTime;
if (mcpcounter <= 0)
{
for (int i = 1; i <= 1; i++)
{
manamanager.mana -= 10;
mcpcounter = mcp;
Instantiate(cp, player.transform.position, player.transform.rotation);
levelManager.currentChekPoint.transform.position = cp.transform.position;
cpenough = true;
}
}
}
}
}
if (Input.GetKeyUp(KeyCode.E))
mcpcounter = mcp;
}
}
Answer by TheVivi · Nov 23, 2019 at 02:34 PM
Input.GetKey gets called every frame try moving everything from Input.GetKey to the Input.GetKeyUp or Input.GetKeyDown like so:
void Update () {
if (Input.GetKeyUp (KeyCode.E)) {
mcpcounter = mcp;
if (manamanager.mana >= 20) {
if (cpenough == false) {
mcpcounter -= Time.deltaTime;
if (mcpcounter <= 0) {
for (int i = 1; i <= 1; i++) {
manamanager.mana -= 10;
mcpcounter = mcp;
Instantiate (cp, player.transform.position, player.transform.rotation);
levelManager.currentChekPoint.transform.position = cp.transform.position;
cpenough = true;
}
}
}
}
}
}
didnt work :( exatly dont work cuz if use getkeyup when key realse time.deltatime will decrease about 0.2 (a fram) and never gone to be 0 from 2.
I understand in that case you can keep it in the Get$$anonymous$$ey but you need to use boolean to say that only happens once like so:
bool once;
void Update () {
ac = player.transform.position.x - level$$anonymous$$anager.currentChekPoint.transform.position.x;
if (ac > 3 || ac < -3)
cpenough = false;
else cpenough = true;
//making chekpoint(cp)
if (player.gd) {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E)) {
mcpcounter = mcp;
once = true;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.E)) {
if (manamanager.mana >= 20 && once) {
if (cpenough == false) {
mcpcounter -= Time.deltaTime;
if (mcpcounter <= 0)
{
for (int i = 1; i <= 1; i++) {
if(once) {
manamanager.mana -= 10;
mcpcounter = mcp;
Instantiate (cp, player.transform.position, player.transform.rotation);
level$$anonymous$$anager.currentChekPoint.transform.position = cp.transform.position;
cpenough = true;
once = false;
}
}
}
}
}
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.E)) {
mcpcounter = mcp;
once = false;
}
}
}
nope.exatly i did same with cpenough . unity alwayse create 3x object .first time 3 ,second 6 and 9 in next ....
Answer by HenriMR · Nov 23, 2019 at 06:49 PM
I think you could use a Coroutine for that. Something like that:
Coroutine coroutine =null;
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
if (manamanager.mana >= 20 )
{
if (cpenough == false)
{
//do your stuff
if (coroutine != null) { StopCoroutine(coroutine); }
coroutine = StartCoroutine (Checkpoint());
}
}
}
}
private IEnumerator Checkpoint()
{
float t = 2;
while (t>0)
{
yield return null;
t -=Time.deltaTime;
if (Input.GetKeyUp)
{
yield break;
}
}
//create check point
}
didnt help :(. i think yeild return use for doing something over and over with some time distance. it help if chek "ifs" but dont work.