Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gendoline · Nov 05, 2018 at 04:40 PM · gameobjectdirectionevent

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to store the last 20 transform.position(vector3s) of an moving object in runtime and make a direction vector out of it? 1 Answer

Locking onto enemies with the third person controller 2 Answers

Fixing instantiated object in one direction? 0 Answers

How to change object direction when Camera Rotates 0 Answers

Fire an event when gameObject has been renamed? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges