[JS] Change sprite when rotated certain way

Okay the title of my question might be a bit confusing but what i want to know is how to do something depending on the rotation of the object. I’m making a 2D rpg where you click to move but since you can’t just rotate the object i have to change the sprite. But my problem is: How do i make the script know which sprite it should load. I only have 4 sprites so it should of course snap to 90 degrees. I tried something like this but it isn’t working as planes (give me wrong numbers)

if((Mathf.Round(Rotation.y)) > 0 && (Mathf.Round(Rotation.y)) < 0.5) {
			Debug.Log("Right " + Rotation.y);
		} else if((Mathf.Round(Rotation.y)) > 0.5 && (Mathf.Round(Rotation.y)) < 1) {
			Debug.Log("Down " + Rotation.y);
		} else if((Mathf.Round(Rotation.y)) < 1 && (Mathf.Round(Rotation.y)) > -0.5) {
			Debug.Log("Left " + Rotation.y);
		} else if((Mathf.Round(Rotation.y)) > -0.5 && (Mathf.Round(Rotation.y)) < 0) {
			Debug.Log("Up " + Rotation.y);
		} else {
			Debug.Log("ERROR " + Rotation.y);
		}

If you have any questions (This might not have been clear enough :P) please let me know.

Edit: Here’s a link to the full code: http://pastebin.com/raw.php?i=UkUeAHrp

You don’t show were you assign Rotation.y, so I’m not sure where you get your values. And since you are looking at ‘Y’ rotation, I’m assuming your app is top down (i.e. the camera looking down the Y axis). One way to solve your problem would be to use Vector3.Angle(). It returns an unsigned angle between two vectors. So using the transform of the object that you are using to detect the angle:

if (Vector3.Angle(transform.forward, Vector3.forward) <= 45.0) {
   Debug.Log("Forward");
} else if (Vector3.Angle(-transform.forward, Vector3.back) <= 45.0) {
   Debug.Log("Back");
} else if (Vector3.Angle(transform.right, Vector3.right) <= 45.0) {
  Debug.Log("Right");
} else if (Vector3.Angle(-transform.right, Vector3.left) <= 45.0) {
  Debug.Log("Left");
} else {
  Debug.Log("Something is messed up");
}

Robertbu posted the answer but later removed it(?) so here it is:

For some reason i can’t post code :frowning:

#pragma strict

//Vertical bar |
var smooth: int;
private var targetPosition: Vector3;
var speed = 2;
var hitRotation;
var hit: RaycastHit;
var marker: GameObject;
var targetPoint : Vector3;
//var rotation;

function Update() {
if (Input.GetButtonDown (“Fire1”)) {
smooth = 1;
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast(ray, hitdist)) {
targetPoint = ray.GetPoint(hitdist);
targetPosition = targetPoint;//ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
}
hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
var go : GameObject = Instantiate(marker, targetPoint, hitRotation);

   if (Vector3.Angle(targetPoint - transform.position, Vector3.forward) <= 45.0) {
      Debug.Log("Forward");
   } else if (Vector3.Angle(targetPoint - transform.position, Vector3.back) <= 45.0) {
      Debug.Log("Back");
   } else if (Vector3.Angle(targetPoint - transform.position, Vector3.right) <= 45.0) {
     Debug.Log("Right");
   } else if (Vector3.Angle(targetPoint - transform.position, Vector3.left) <= 45.0) {
     Debug.Log("Left");
   } else {
     Debug.Log("Something is messed up");
   }
}

var dir: Vector3 = targetPosition - transform.position;
var dist: float = dir.magnitude;
var move: float = speed * Time.deltaTime;
if (dist > move) {
    transform.position += dir.normalized * move;
} else {
    transform.position = targetPosition;
}
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;

}

And if you ever see this robertbu Thanks you so much! I’ve tried to fix this for ages :smiley: