Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 buttholejohnson · Nov 19, 2014 at 06:03 PM · collisionobjectstags

Calling 2 objects into script for onCollisionEnter?

Basically have a moving platform that is working great, and now I am trying to get a trigger going so that when the player enters it then the platform starts its animation of going back and forth as it does. I want to be able to do this through the platform script to keep things more modular, but cannot figure out how the right way to use the two objects of player and P3Trig in conjuction with OnTriggerEnter2D. The following is my failed attempt:

 using UnityEngine;
 using System.Collections;
 
 public class Pmove3 : MonoBehaviour {
 public GameObject Player;
     public GameObject P3Trig;
     
     if (Player){
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.tag == "P3Trig")
         {
             moveStart = true;
             Pmove3Control ("Start");
     }
 }

Thanks a million! Look forward to paying the help forward here.

Comment
Add comment · Show 1
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
avatar image RayJr · Nov 19, 2014 at 06:11 PM 0
Share

So the trigger is a seperate component on the same object as the collider?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RayJr · Nov 19, 2014 at 06:17 PM

If the answer to the above comment is true, you can get the 'mover' script like so - replace "Mover" with the name of the script doing the moving.

 Mover moveScript = GetComponent.<Mover>  ();

If the script is on a different object then the trigger, you'll need to get the platform object first. you're better off putting the trigger on the same object though, so you don't need to specify the platform.

 gameObject oPlatform = Find.GameObject ("Platform");
 Mover moveScript = oPlatform.GetComponent.<Mover>  ();

with that you can set a variable or call a function on that script like so

 moveScript.Move = true;

or

 moveScript.StartMove();

just make sure the function and variables are set to public. Also, i haven't compiled this, so you might need to modify it a bit to work

Comment
Add comment · Show 6 · 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
avatar image buttholejohnson · Nov 19, 2014 at 07:37 PM 0
Share

I think we had some misunderstandings....Here is the entire code:

     using UnityEngine;
     using System.Collections;
     
     public class Pmove3 : $$anonymous$$onoBehaviour {
     
         public GameObject Player;
         public GameObject P3Trig;
         Animator animator;
         bool moveStart;
     
         void Start ()
         {
             moveStart = false;
             animator = GetComponent<Animator>();
         }
     
         void OnTriggerEnter2D(Collider2D col)
         {
             if(col.gameObject.tag == "P3Trig")
             {
                 moveStart = true;
                 Pmove3Control ("Start");
         }
     }
         void OnTriggerExit2D(Collider2D col)
         {
             if(moveStart)
             {
                 moveStart = false;
                Pmove3Control ("Return");
                }
      }
 
 
      void Pmove3Control(string direction)
            {
             animator.SetTrigger(direction);
         }
 }


This IS the platform move code, and it is attached to the platform object. By 'mover,' do you mean player? I wanted to have all this script contained inside the platform object so I don't have to write more script into the main players script to correspond with every new trigger for every new platform. I figure that if the platform script can call both the player, who is to trigger the trigger, and the trigger- then I can make as many moving, trigger-able platforms as I want without having to further modify the player script. I called them both in, I know how to do that, but I do not know how to tie the OnTriggerEnter2D to the Player to complete the script. Just the lines with the OnTriggerEnter2D if collide with P3 Trig needs to be fixed to something like:

void --->PLAYER<---OnTriggerEnter2D(Collider2D col) { if(col.gameObject.tag == "P3Trig") {

So that player is somehow in that line. I appreciate the help soooo much in advance thank you.

avatar image RayJr · Nov 19, 2014 at 08:34 PM 0
Share

If this is on the platform, then 'col' will be the player once he lands on it, no?

if you take your Enter collider function and temporarily update it to :

  void OnTriggerEnter2D(Collider2D col)         

{ Debug.Log (col.gameOjbect.name) }

You should see the name of your player. is your player have it's tag set to P3Trig? That might be the problem. Name and Tag are different things.

I'm not at my development computer right now, I don't have unity in front of me. I'll take a look later when i'm home if its not solved by then.

avatar image buttholejohnson · Nov 19, 2014 at 10:47 PM 0
Share

No- Player, is the player, tagged as Player. P3Trig is the trigger, named and tagged P3Trig. They are both being called into a script which is on the platform.

public GameObject Player; public GameObject P3Trig;

Player needs to trigger the trigger (P3Trig). Can this even be done? I'm getting a feeling that maybe not?

avatar image RayJr · Nov 20, 2014 at 01:19 AM 0
Share

Yes it can. In your OnTriggerEnter2D , 'col' will be the player object (or anything else that touches it). It will never be itself ( P3Trig). So your if statement will not run. Update the if statement to check for the player object's tag.

On void OnTriggerExit2D, update that if statement as well to check for the player's tag. If it is true, that means the player is leaving the platform. Don't bother checking if $$anonymous$$oveStart is set to true, if the player is leaving the platform, it will be.

try this..

 using UnityEngine;
 using System.Collections;
 
 public class Pmove3 : $$anonymous$$onoBehaviour {
     
     public GameObject Player;
     public GameObject P3Trig;
     Animator animator;
     bool moveStart;
     
     void Start ()
     {
         moveStart = false;
         animator = GetComponent<Animator>();
     }
     
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.name == "Player")
         {
             moveStart = true;
             Pmove3Control ("Start");
         }
     }
     void OnTriggerExit2D(Collider2D col)
     {
         if(col.name == "Player")
         {
             moveStart = false;
             Pmove3Control ("Return");
         }
     }
     
     
     void Pmove3Control(string direction)
     {
         animator.SetTrigger(direction);
     }
 }
 

avatar image buttholejohnson · Nov 20, 2014 at 03:32 AM 0
Share

Stiiiill appear to not be understanding each other I think sorry...The script has nothing to do with the player being on the platform. It should do the following: 1. Player enters P3Trig 2. Platform starts it's animation of moving back and forth.

It looks like you just replaced

 if(col.name =="P3Trig") 

with

 if(col.name =="Player") 

So now, correct me if I am mistaken, there is no longer any reference to the trigger, P3Trig????

Show more comments

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

27 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

Related Questions

Best way to manage a collision between a few different objects? 1 Answer

Collision detection of certain ojects 1 Answer

How to make a specific object not collide with a specific collider - 2D 1 Answer

Preventing objects from shoved 0 Answers

OnParticleCollision 1 Answer


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