- Home /
transform.localscale not working after update
Hello
I have a script for my enemy AI which flips the object and applies velocity to it (moves it left or right) when it hits an obstacle. I recently updated Unity to the newest version, before the update this worked like a dream but now on hitting the obstacle the AI turns correctly but continues in his previous direction anyway. I'm very confused as I have changed absolutely nothing. If anyone can see any mistakes I may have made or anything I would be very appreciative, I'm at my wits end right now.
Here is the method for turning the AI
void turn(float prevDirection, float newDirection)
{
if (walkingDirection == prevDirection)
{
walkingDirection = newDirection;
transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, 1);
if (walkingDirection == 1)
{
isFacingRight = true;
}
else if (walkingDirection == -1)
{
isFacingRight = false;
}
}
}
here is the method for applying the movement to the AI
void Move(float speed)
{
velocity.x = speed * Time.deltaTime;
velocity.y += gravity * Time.deltaTime;
controller.Move(walkingDirection * velocity * Time.deltaTime);
}
and finally the if statement (in the update method) where these are applied:
if(!alert && !caution)
{
spr.color = Color.blue;
Move(100);
positionSet = false;
alertCoolDown = 1;
if (controller.collisions.right)
{
turn(1, -1);
}
else if (controller.collisions.left)
{
turn(-1, 1);
}
}
I have included a .gif of the error in question so hopefully you can see what I mean: https://s21.postimg.org/tv7wcqyef/bug.gif
ANY help at all would be amazing. Thank you very much.
@ScarcticCG I have the same error. Did you find a fix for this?
Answer by ScarcticCG · Nov 01, 2016 at 04:36 PM
@donlew I certainly did! Had to email customer support to find a fix, which is bananas and probably says a lot about Unity Documentation. Anyway, I will c+p the email here for your convenience.
So I have talked with the developer and it seems that "transform.Translate(velocity);" has changed behaviour since 5.4(The X velocity is -0.04f but when added to the transform that has local scale X to -1, it causes the Transform to be moved right and not left as previous.)
Here is a fix for your issue:
There is now an overload of this method that allows you to specify which space to translate in i.e. self (local) or world.
https://docs.unity3d.com/ScriptReference/Transform.Translate.html
https://docs.unity3d.com/ScriptReference/Space.html
Changing the method from:
transform.Translate(velocity);
to
transform.Translate(velocity, Space.World);
seems to solve the issue.
Hope this helps you!