Calling a function through EventTrigger does not loop.
Hello, I am calling a function through the new Unity UI system but it is not looping my function. I have attached my game object to the event trigger and the function I am calling is "TurnLeft". I am using the On Pointer Down event so it should play continuously.
Here is the function as well. I am trying to make my character rotate using the new UI system. Please help, I can't seem to find the solution.
function TurnLeft(){
transform.Rotate(Vector3(0,-1,0)*turnSpeed* Time.deltaTime);
}
Answer by seth_slax · Oct 23, 2015 at 04:48 AM
OnPointerDown is essentially the same as GetKeyDown, in that it only triggers when the key is initially pressed. To loop this, I'd suggest having the OnPointerDown switch on a boolean. Something along these lines:
var turn : boolean;
function Update(){
if(turn){
TurnLeft();
}
}
function Turn(which : boolean){
turn = which;
}
function TurnLeft(){
transform.Rotate(Vector3(0,-1,0)*turnSpeed* Time.deltaTime);
}
And then have your OnPointerDown call the Turn() function, specifying if you want it on or not.
Can I use the turn variable to turn right as well or would I have to create another variable to turn right? Thanks. Ie. if turn = left { TurnLeft () }
Depends, really. If you want it to always turn left or right, you could keep to one boolean, or have two to control them independantly. Booleans only have a true or false state. Alternatively, you could use an int . For example 0 = no movement, 1 = left and 2 = right, or do something similar with a string. Hope this helps!
Great! Thanks for all the help. $$anonymous$$uch appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612042349im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Massive lag spike on first Input.GetTouch with nothing on scene but one script (Android) 2 Answers
Mouse or Touch events independent of framerate 0 Answers
Sprite/gameObject touch or click event 0 Answers
Detect if mouse has clicked on a UI element? 0 Answers
OnDrag is moving all colliders on Touch? (Not on Standalone) 0 Answers