Hi guys,
I have a Noobish error that is hard to explain but I’ll try my best. I am using C#
I have a line of code that translates a gameObject with a material on it that I’m using to make it look like its shooting a bullet forward.
transform.Translate (Vector3.right* Time.deltaTime *moveSpeed);
Now to shoot left and right in my 2D Platformer, On keypress I would change Vector3.right
to Vector3.left
To determine if I’m facing Left or right I have made a bool that already checks if im facing left or right by checking the GameObject.Position.localScale.x if it is (-1,1,1)
or (1,1,1).
of my character.
The end code will look like this.
if (facingRight == true) {
transform.Translate (Vector3.right* Time.deltaTime *moveSpeed);
}
else
{
transform.Translate (Vector3.left* Time.deltaTime *moveSpeed);
}
Which looks simple and it does work, kinda. - the Problem I’m having is that once the Sprite is busy being translated over time (moving forward) and If I turn the character and the bool updates to facingRight = false. Then the gameObjects will start translating in the other direction in mid air.
I know this is because its checking the bool inside the update function so it is updating every frame. But how would I Check the bool from outside the Update(); function and call just
transform.Translate (Vector3.left* Time.deltaTime *moveSpeed);
which needs to be updated every frame. Can i make that a variable or something? I know I cant create it as a function WITHIN the update Function.
I hope this makes sense.
Razz