- Home /
2D - Sprite based on angle
Is there a way to get the Z rotation of the transform and set the corresponding sprite for my character? So when a target is moving down, then player can see it's face, when the target is moving up, player can see it's back etc...like in starcraft.
So far i was trying to get eulerAngles of my target's transform, and then compare Z with a float...which doesnt work apparently=/ it does not show any errors whatsoever, as if the numbers just never matched.
#pragma strict
var spriteLeft : Sprite;
var spriteRight : Sprite;
var spriteUp : Sprite;
var spriteDown : Sprite;
function Start () {
}
function Update () {
var angle = transform.eulerAngles;
if(angle.z <= 0.0) { /*gameObject.GetComponent.<SpriteRenderer>().sprite = spriteLeft;*/ Debug.Log("one"); }
if(angle.z >= 0.0) { /*gameObject.GetComponent.<SpriteRenderer>().sprite = spriteRight;*/ Debug.Log("two"); }
//if(gameObject.transform.eulerAngles.z >= 90f) { gameObject.GetComponent.<SpriteRenderer>().sprite = spriteLeft; }
//if(gameObject.transform.eulerAngles.z >= 0f) { gameObject.GetComponent.<SpriteRenderer>().sprite = spriteUp; }
Debug.Log(transform.eulerAngles.z + "Angle: " + angle.z);
}
Are you getting a numeric angle.z value in that final Debug.Log? No numeric value should fail both >=0 and
yes, actually i checked and, it seems that the condition is ok, i am getting the debug message "one" or "two" (with proper angles) - but the sprite refuses to change
Okay, first off, just in case, are you aware that your sprite changing code is entirely commented out?
yus, i was trying to just get the debug message to start with, and it does show up, sorry i had to revert the code to sprite-selection, but nontheless this is a statement im trying to change the sprite with
Your answer
Follow this Question
Related Questions
Animation doesn't play all the way through unless holding the key down 1 Answer
A Question about sprites 3 Answers
How to hide game window but show sprite? 0 Answers
Move Object With Angle In 2D 1 Answer