Update Problem with Function inside Update()

I want my character to look at the enemy:

function UpdateSmoothedMovementDirection(){
...
var forward = lockOnTarget.TransformDirection(Vector3.forward);
...
}



Update(){

UpdateSmoothedMovementDirection();

if(Input.GetButton("LockOn")){
lockOn = true;
}
else{
lockOn = false;
}

It works very well this way, but when i put it this way:

(inside the UpdatedSmootedMovementDirection())
if(lockOn){
var forward = lockOnTarget.TransformDirection(Vector3.forward);
}

It doesn’t update it every frame like the first one. I have to pull my finger and press the button every time i want to update the direction of my character faces.

because you need to press that button "LockOn" !!! if if you don't press it it makes lockon false witch means as soon as you release that button it will make LockOn bool false witch means it will now go through if(lockOn){ var forward = lockOnTarget.TransformDirection(Vector3.forward); } but it'll skip it because lockOn is false

1 Answer

1

It won’t update every frame when you wrap it in the if(lockOn) because lockOn has to be true for it to work if you do that, so it is only true if you click the button. What are you trying to achieve by wrapping it with the if(lockOn)? If you want lockOn to remain true after clicking the button, then remove the else lockOn = false.

Also, you don’t need to precede your call to UpdateSmoothedMovementDirection(); in Update with the “function” name. I don’t believe that should even compile…

why wouldn't it update it every frame? it is in update so it'll update lockOn every frame and if he does not keep his finger on that button than it will update lockOn to the false and why wouldn't Update be able to call functions? well I'm no good at unity script but I call functions from update all the time

oh yeah propably you are right inside Update() function UpdateSmoothedMovementDirection(); I'm not good at java tho I write c#

I want to lock on only if player holds down space.