The question is answered, right answer was accepted
How to move and rotate to other direction when hitting a wall
I have some enemies in my game that i want them moving either horizontally or vertically and changing direction when hitting a wall. (2D game, sprite attached to empty game object and the empty game object has RigidBody2D and Circle Collider).
I want on OnTriggerEnter2D method to change the rotation of my sprite and collider in order to use only one sprite (and now 4 for the different directions) but I cant handle quaternions very well.
I am posting the code of a previous version where I used 4 sprites and changed them according to the direction.
 using UnityEngine;
 using System.Collections;
 
 public class GhostBehaviour : MonoBehaviour {
 
     private enum Direction
     {
         LEFT,
         RIGHT,
         UP,
         DOWN
     }
 
     public float speed;
     private float speedX;
     private float speedY;
 
     private Direction direction;
 
     public Sprite spriteGhostUp;
     public Sprite spriteGhostDown;
     public Sprite spriteGhostLeft;
     public Sprite spriteGhostRight;
 
     // Use this for initialization
     void Start () {
         speedX = 0;
         speedY = 0;
 
         direction = (Direction)Random.Range (0, 4);
 
         switch (direction) {
         case Direction.LEFT:
             speedX = -speed;
             GetComponent<SpriteRenderer>().sprite = spriteGhostLeft;
             break;
         case Direction.RIGHT:
             GetComponent<SpriteRenderer>().sprite = spriteGhostRight;
             speedX = speed;
             break;
         case Direction.UP:
             GetComponent<SpriteRenderer>().sprite = spriteGhostUp;
             speedY = speed;
             break;
         case Direction.DOWN:
             GetComponent<SpriteRenderer>().sprite = spriteGhostDown;
             speedY = -speed;
             break;
         }
     }
 
     // FixedUpdate is called once per frame
     void FixedUpdate(){
         transform.Translate (Time.deltaTime * speedX, Time.deltaTime * speedY, 0.0f);
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.gameObject.tag == "Wall") {
             speedX *= -1;
             speedY *= -1;
 
             switch(direction){
             case Direction.LEFT:
                 direction = Direction.RIGHT;
                 break;
             case Direction.RIGHT:
                 direction = Direction.LEFT;
                 break;
             case Direction.UP:
                 direction = Direction.DOWN;
                 break;
             case Direction.DOWN:
                 direction = Direction.UP;
                 break;
             }
         }
     }
 
     // Update is called once per frame
     void Update () {
         switch (direction) {
         case Direction.LEFT:
             GetComponent<SpriteRenderer>().sprite = spriteGhostLeft;
             break;
         case Direction.RIGHT:
             GetComponent<SpriteRenderer>().sprite = spriteGhostRight;
             break;
         case Direction.UP:
             GetComponent<SpriteRenderer>().sprite = spriteGhostUp;
             break;
         case Direction.DOWN:
             GetComponent<SpriteRenderer>().sprite = spriteGhostDown;        
             break;
         }
     }
 
     public void Freeze(){
         GetComponent<SpriteRenderer> ().color = new Color (0.68f, 1.0f, 1.0f);
         SetSpeed (1);
     }
 
     public void UnFreeze(){
         GetComponent<SpriteRenderer> ().color = new Color (1.0f, 1.0f, 1.0f);
         SetSpeed (4);
     }
 
     void SetSpeed(int s){
         if (speedX > 0) speedX = s;
         if (speedX < 0) speedX = -s;
         if (speedY > 0) speedY = s;
         if (speedY < 0) speedY = -s;
     }
 }
and this is what I came up with this time
public class GhostMovement : MonoBehaviour {
 enum Direction{ UP, DOWN, LEFT, RIGHT};
 private Direction direction;
 public float speed;
 void Start () {
     direction = InitDirection ();
     SetDirection (direction);
     SetRotation (direction);
 }
 void Update () {
     transform.Translate (speed * Time.deltaTime * Vector2.right);
 }
 void OnTriggerEnter2D(Collider2D other){
     if (other.CompareTag ("Boundary")) {
         speed *= -1;
         if (direction == Direction.UP){
             SetDirection (Direction.DOWN);
             SetRotation (Direction.DOWN);
         }
         if (direction == Direction.DOWN){
             SetDirection (Direction.UP);
             SetRotation (Direction.UP);
         }
         if (direction == Direction.LEFT){
             SetDirection (Direction.RIGHT);
             SetRotation (Direction.RIGHT);
         }
         if (direction == Direction.RIGHT){
             SetDirection (Direction.LEFT);
             SetRotation (Direction.LEFT);
         }
     }
     Debug.Log (direction);
 }
 
 Direction InitDirection(){
     System.Array enumArray = System.Enum.GetValues (typeof(Direction));
     return (Direction)enumArray.GetValue (Random.Range (0, enumArray.Length));    
 }     
 void SetDirection(Direction d){
     switch (d){
     case Direction.UP:
         direction = Direction.UP;
         break;
     case Direction.DOWN:
         direction = Direction.DOWN;
         break;
     case Direction.LEFT:
         direction = Direction.LEFT;
         break;
     case Direction.RIGHT:
         direction = Direction.RIGHT;
         break;
     }
 }
 void SetRotation(Direction d){
     switch (d){
     case Direction.UP:
         transform.rotation = Quaternion.Euler(0,0,90);
         break;
     case Direction.DOWN:
         transform.rotation = Quaternion.Euler(180,0,90);
         break;
     case Direction.LEFT:
         transform.rotation = Quaternion.Euler(0,180,0);
         break;
     case Direction.RIGHT:
         transform.rotation = Quaternion.Euler(0,0,0);
         break;
     }
 }
 void ReverseDirection(){
     if (direction == Direction.UP)
         SetDirection (Direction.DOWN);
     if (direction == Direction.DOWN)
         SetDirection (Direction.UP);
     if (direction == Direction.LEFT)
         SetDirection (Direction.RIGHT);
     if (direction == Direction.RIGHT)
         SetDirection (Direction.LEFT);
 }
 void ReverseRotation(){
     if (direction == Direction.UP)
         SetRotation (Direction.DOWN);
     if (direction == Direction.DOWN)
         SetRotation (Direction.UP);
     if (direction == Direction.LEFT)
         SetRotation (Direction.RIGHT);
     if (direction == Direction.RIGHT)
         SetRotation (Direction.LEFT);
 }
}
Please help! Thanks in advance!
Answer by parisk85 · Feb 19, 2016 at 06:31 PM
Problem solved!
 using UnityEngine;
 using System.Collections;
 
 public class GhostMovement : MonoBehaviour {
 
     enum Direction{ UP, DOWN, LEFT, RIGHT};
     private Direction direction;
 
     public float speed;
 
     void Start () {
         direction = new Direction();
         direction = InitDirection ();
         SetDirection (direction);
         SetRotation (direction);
     }
 
     void FixedUpdate () {
         transform.Translate (speed * Time.deltaTime * Vector2.right);
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.CompareTag ("Boundary")) {
             switch (direction){
             case Direction.UP:
                 SetDirection(Direction.DOWN);
                 transform.eulerAngles = new Vector3(180, 0, 90);
                 break;
             case Direction.DOWN:
                 SetDirection(Direction.UP);
                 transform.eulerAngles = new Vector3(0, 0, 90);
                 break;
             case Direction.LEFT:
                 SetDirection(Direction.RIGHT);
                 transform.eulerAngles = new Vector3(0, 0, 0);
                 break;
             case Direction.RIGHT:
                 SetDirection(Direction.LEFT);
                 transform.eulerAngles = new Vector3(0, 180, 0);
                 break;
             }        
         }
     }
     
     Direction InitDirection(){
         System.Array enumArray = System.Enum.GetValues (typeof(Direction));
         return (Direction)enumArray.GetValue (Random.Range (0, enumArray.Length));    
     }     
 
     void SetDirection(Direction d){
         switch (d){
         case Direction.UP:
             direction = Direction.UP;
             verticalSpeed = -speed;
             break;
         case Direction.DOWN:
             direction = Direction.DOWN;
             verticalSpeed = speed;
             break;
         case Direction.LEFT:
             direction = Direction.LEFT;
             horizontalSpeed = -speed;
             break;
         case Direction.RIGHT:
             direction = Direction.RIGHT;
             horizontalSpeed = speed;
             break;
         }
     }
 
     void SetRotation(Direction d){
         switch (d){
         case Direction.UP:
             transform.rotation = Quaternion.Euler(0,0,90);
             break;
         case Direction.DOWN:
             transform.rotation = Quaternion.Euler(180,0,90);
             break;
         case Direction.LEFT:
             transform.rotation = Quaternion.Euler(0,180,0);
             break;
         case Direction.RIGHT:
             transform.rotation = Quaternion.Euler(0,0,0);
             break;
         }
     }
     
 }
 
Follow this Question
Related Questions
Trying to get bool statement to work in OnTriggerStay2D 1 Answer
How do i make my bullet destroy on trigger? 1 Answer
Rotation (Quaternion.Lerp) not working 0 Answers
Can't seem to get collision to work among my sprites 1 Answer
Smooth look at 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                