- Home /
How to decrease gravityScale in period of time?
Hello. I wanna ask something. I want to make if an object1 is clicked, gravityScale of object2 is decreased.
I tried write code to detect if object1 was clicked or not, using boolean in while looping in void Update() like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VelocityDecrease : MonoBehaviour
{
public static bool hitDisinfektan = false;
int countDown = 3;
void Update()
{
...
if (hit.collider.gameObject == gameObject) //hit is variabel in RaycastHit2D
{
Destroy(gameObject);
while(countDown > 0)
{
hitDisinfektan = true;
Debug.Log("Countdown : " + countDown);
countDown--;
}
}
}
}
hitDisinfektan = false;
}
}
I passing that script in an object as prefab object1.
Then in object2, I wrote this code :
void Update()
{
if(VelocityDecrease.hitDisinfektan == true)
{
DecreasingGravity();
}
rbvirus.gravityScale = 1;
}
void DecreasingGravity()
{
rbvirus.gravityScale = 0;
}
But, hitDisinfektan
not become true
. How to make that true in 3 seconds when object1 is clicked and then change it to false again? I am looking forward anyone help me. Thanks.
Answer by MakerBen · Jul 18, 2020 at 08:27 PM
You are counting 3 frames so it does become true for a really short period of time try changing countDown--; to countDown = countDown - Time.DeltaTime;
Okay, but if I using while
looping, that happen is infinite loop. What should I use for looping?
Your answer
Follow this Question
Related Questions
Loop Coroutine For A Demo Mode 1 Answer
Yield WaitForSeconds not working > once in coroutine 1 Answer
Yield/OnTriggerEnter Help C# 1 Answer
Call another function during wait for seconds 1 Answer
WaitForSeconds using Update? 1 Answer