how to make a statement happen only once in a certain condition
I am trying to make my character in Unity 2D be able to hang onto an edge until I press a button and it will either climb up or fall down depending on what you press, but until you press something it just hangs there, and to make sure it doesnt fall down I made the gravityscale 0 and the velocity in both axis 0 as well, once you press W it will go back to normal and pull you up, but the problem is that it would make the velocity 0 and gravity scale 0 on each frame, I need it to happen only on the first frame that it detects that you are on a ledge (using a raycast), every frame after that it doesnt need to update it until I either press W or S feel free to ask any clarifying questions because I am not very good at explaining things
this is the first script(I wrote this in two scripts to keep it a bit more organized):
// name of the raycast
if (hangEdge)
{
if (Input.GetKey(KeyCode.W))
{
rb.gravityScale = 5;
transform.position = Vector3.Lerp(transform.position, myPosition[posIndex], lerpTime * Time.deltaTime);
t = Mathf.Lerp(t, 1f, lerpTime * Time.deltaTime);
if (t > 0.9f)
{
t = 0f;
posIndex++;
posIndex = (posIndex >= lengtht) ? 0 : posIndex;
}
}
}
second script:
if (hangEdge) {
//get rigidbody component(rb is the name of a rigidbody2D variable)
rb.GetComponent<Rigidbody2D>();
if (myFunctionCalled == false)
{
//change velocity on both x and y axis to 0(essentially tell player to stop moving)
rb.velocity = new Vector2(0, 0);
//set the gravity on this object(the player) to 0
rb.gravityScale = 0;
myFunctionCalled = true;
}
}
else
{
//set gravity on this object(the player) to 5
rb.gravityScale = 5;
}
I also made my own attempt at solving the problem, to no avail