- Home /
How to change a Player direction by colliding a Game Object
Hello Everyone , im not used to ask help but i'm getting stuck in my code so ...
The code below is applied to a Game Object "Player" : When i hit "Play" the Player is moving up until its collides with a "Reflector" ,( a gameobject with a collider2d component) then its change direction depending of which kind of "Reflector" its encounters ...
 public class playerMove : MonoBehaviour
 {
    
     Vector3 myVector;
     Rigidbody2D m_Rigidbody;
     [SerializeField] float m_Speed = 2.0f; 
 
     void Start()
     {
 
         myVector = new Vector3(0.0f, 1.0f * m_Speed, 0.0f);      
 
       m_Rigidbody = GetComponent<Rigidbody2D>();     
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.tag == "ReflectorLeft")
         {
           myVector = new Vector3(-1.0f * m_Speed, 0.0f, 0.0f); // if Player touch reflector its turns "Left"
         }
      
     if (col.gameObject.tag == "ReflectorRight")
         {
             myVector = new Vector3(1.0f * m_Speed, 0.0f, 0.0f);
          }
 }
Its working well , but i would like to apply the change of direction directly from a script stored in each kind of reflector and not in the PlayerMove only .
 public class ReflectorLeft : MonoBehaviour
 {
     public GameObject selectPlayer; // Store Player prefab
     
     void OnTriggerEnter2D(Collider2D other) 
     {
         if (other.gameObject.tag == "Player")
         { 
            selectPlayer.  ** change direction to left**
          }
 }
What should i add in my ReflectorLeft.cs ? (sorry for my english) Thanks by advance Gwendoline
Answer by AaronXRDev · Nov 05, 2018 at 05:15 PM
I would create a generic "Reflector" class and use an enum so you have a dropdown on each reflector to choose the type. Like this:
 public class Reflector : MonoBehaviour
     {
         public enum ReflectorType
         {
             Left,
             Right
         }
 
         public ReflectorType reflectorType; //Choose reflector type
         public GameObject selectPlayer; // Store Player prefab
 
         void OnTriggerEnter2D(Collider2D other)
         {
             if (other.gameObject.tag == "Player")
             {
                 switch (reflectorType)
                 {
                     case ReflectorType.Left:
                         selectPlayer.SendMessage("ReflectTowards", new Vector3(-1.0f, 0.0f, 0.0f));
                         // In the ReflectTowards method, multiply the whole Vector3 by the m_Speed
                         break;
                     case ReflectorType.Right:
                         selectPlayer.SendMessage("ReflectTowards", new Vector3(1.0f, 0.0f, 0.0f));
                         // In the ReflectTowards method, multiply the whole Vector3 by the m_Speed
                         break;
                 }
             }
         }
     }
On the player, you would handle the method like this: 
         public void ReflectTowards(Vector3 newVector)
         {
             myVector = newVector * m_Speed;
         }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                