- Home /
Getting Sprites to Face Eachother (building on 2D Platformer tut)
Simply put, I'm building on the Basic Platformer tut and am trying to get it so two characters always have their sprites facing eachother, rather than flipping when the player moves. I figured this would easily be done by checking in ones script if the other character is further right from them, and if they're not already facing right to flip using the flipping script already in the tutorial. This is how I thought I'd do that;
if (hero2.transform.position.x > transform.position.x && !facingRight)
flip
With hero2 being the gameObject for the other character. To my understanding, that should;
Get the x value of hero2's transform's world position
Check if it is larger than the x value of the world position of the transform of whatever gameObject the script is attached to
Check if facingRight is false
Run the function to flip the sprite if both are true
Instead, when I run the game it doesn't work and barrages me with error messages citing that line trying to access a GameObject not present on the character. I can't figure out what it's trying to access - the hero2 gameObject is set to the proper thing, and adding an empty gameObject to the character doesn't change anything. Thoughts? As I said, this is based on the scripts/assets used in the basic platformer tutorial.