- Home /
How to flip based on x pos properly?
if(gameObject.transform.position.x >= 0)
{
Vector3 bugbotScale = transform.localScale;
bugbotScale.x *= -1;
transform.localScale = bugbotScale;
}
I'm trying to flip based on which side it is on screen. I works fine when not flipping but when it does it doesn't stop. It just keeps flipping
how about:
if(gameObject.transform.position.x >= 0 && bugbotScale.x >=0){
The flip worked perfectly but then the colliders stop working and I can't hit them anymore.
colliders attached to 'bugbot' or attached to the other objects, please explain 'and I can't hit them anymore'.
Whenever I add the code to the the script on bugbot It seems as though the trigger on my bat doesn't register when it hits the polygon collider of bugbot.
I think it has to do with this line on my bat
Transform BugbotTransform = Bugbot.transform;
and this error comes out in the console
NullReferenceException: Object reference not set to an instance of an object Bat.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Scripts/Bat.cs:19)
Can you provide the code that the error message is referring to?
Answer by Ed unity · Apr 21, 2014 at 07:18 PM
Negative scale is something that you should always try to stay away from when working with colliders. Try this instead:
if(gameObject.transform.position.x >= 0)
{
Vector3 newbugbotPosition = new Vector3(-transform.position.x, transform.position.y, transform.position.z);
transform.position = newbugbotPosition;
transform.Rotate(Vector3.up * 180, Space.World);
}
This should properly adjust the collider when the object flips and rotate it to face the opposite direction as well.