- Home /
Question by
myyokeisheavvy · Jan 29, 2020 at 03:26 PM ·
transform.positiontriggers
Change position of an object on a keystroke within a trigger only?
Probably a newbie questions but I would like to transform an object's position on a key input, but only have this work within a particular trigger.
Thank you!
Comment
Answer by knobblez · Jan 29, 2020 at 05:55 PM
void OnTriggerStay()
{
if (Input.GetKey(KeyCode.A))
{
//change position
}
}
OnTriggerStay() is called most but not all frames so you might want to set a bool to true inside OnTriggerStay() and check for input and the bool inside Update()
Yes, you can do this, but as he said add a bool.
Set it to true with OnTriggerEnter()
Set it to false with OnTriggerExit()
and then check that input in the Update() method as it is called every frame.
Thanks! How would I go about setting a bool? I'm sorry, I'm extremely new to coding. @mwaterman29 @knobblez