Moving back and fourth but wont face the right direction

If anyone knows what i am messing up here it would be really appreciated.

Ok so my game is in 2d and has a lot of little baddies that just do a basic back and fourth along x

for both my main character and all the baddies, I basically have them set up as a parent that has all the controls. The graphic is a child with just sprite manager2.

My main character is working fine since his direction is based of the movement controlled but the virtual joystick

However, I cant figure out how to get the bad guys to rotate based on movement like the main guy does.

Here are my two scripts

  1. the basic script for bad guy to go back and fourth on the Parent.
var targetA : GameObject;
var targetB : GameObject;


var speed : float = 0.1;

function Update () {
	var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5;
	transform.position = targetA.transform.position * weight
						+ targetB.transform.position * (1-weight);
					
}
  1. my script that is on the child SM2 object for snapping back and fourth based on movement.
    I select the parent that is moving for my variable but it doesnt work.
var rb : Rigidbody;
private var moveDirection : Vector3 = Vector3.zero;

 function Update () {
    if ( rb.velocity.x > 0 )  {
        transform.eulerAngles = Vector3(0,0,0);
        
    }
    
     if ( rb.velocity.x < 0 ) {
        transform.eulerAngles = Vector3(0,180,0);
        
    }
}

Thanks a bunch if anyone can help

Ok, i found another solution that is working great with no math lol
I’m posting just incase someone else is looking for an easy way for a 2d sprite to patrol and face the right way and is running into the same problem.
Since my bad guys have a point A and point B they are moving in between
I just have each point send a message to face left or right

One of the message scripts- of course the other one is "rotateFaceLeft

function OnTriggerEnter (other : Collider) {
	other.gameObject.SendMessage ("rotateFaceRight", SendMessageOptions.DontRequireReceiver);
}

The scripts to actually face them the right way - only on the sprite (had to add a box collider)

     function rotateFaceLeft () {
	     transform.eulerAngles = Vector3(0,180,0);
}

      function rotateFaceRight () {
	     transform.eulerAngles = Vector3(0,0,0);
}