Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help with collider triggers..

Discussion in 'Scripting' started by Lmaryon, Jul 21, 2014.

  1. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    Code (CSharp):
    1. public class PlatformScript : MonoBehaviour {
    2.  
    3.     public float speed  = 6.0f;
    4.     Transform player;
    5.     Transform obstacle;
    6.  
    7.  
    8.  
    9.     void start () {
    10.         player = GameObject.FindGameObjectWithTag("Player").transform;
    11.         obstacle = GameObject.FindGameObjectWithTag("Obstacle").transform;
    12.     }
    13.  
    14.     void OnTriggerEnter2D(Collider2D other){
    15.  
    16.  
    17.     }
    18.  
    19.     void Update () {
    20.  
    21.         transform.position += Vector3.left * speed * Time.deltaTime;
    22.    
    23.     }
    24. }
    25.  

    This is what I have so far. I want the Platform object to detect when the player object and obstacle object have collided then stop the vector3.left command until they are no longer colliding, then resume the command.
    how would I do this? Would I have to use ray casting?
     
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Code (js):
    1.  
    2. var canMove : boolean = true;
    3.  
    4. function Update ()
    5. {
    6.      if (canMove)
    7.      {
    8.           transform.position.x = Vector stuff
    9.      }
    10. }
    11. function OnTriggerEnter (trig : Collider)
    12. {
    13.      canMove = true;
    14. }
    15. function OnTriggerExit (trig : Collider)
    16. {
    17.      canMove = false;
    18. }
    19.  
    20.  
     
    Lmaryon likes this.
  3. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    needs to be csharp
     
  4. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    you can't translate?
     
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Code (csharp):
    1.  
    2. bool canMove = true;
    3.  
    4. public float speed =  6.0f;
    5. Transform player;
    6. Transform obstacle;
    7.  
    8. void Update ()
    9. {
    10.      if (canMove)
    11.      {
    12.           transform.position.x += Vector3.Left * speed * Time.deltaTime;
    13.      }
    14. }
    15. void OnTriggerEnter (Collider trig)
    16. {
    17.      canMove = false;
    18. }
    19. void OnTriggerExit (Collider trig)
    20. {
    21.      canMove = true;
    22. }
     
    Lmaryon likes this.
  6. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    Thanks mate, was having a go at translating after i posted the reply. probably could of done it tbh but thanks for doing it. much obliged
     
  7. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    No problem :)
     
  8. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18

    still having a bit of trouble. Can't figure out how to tell the PlatformScript that player and obstacle have collided. Sorry v new to this
     
  9. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Can you tell me what is hitting what, what has colliders, whether or not they are triggers, and where your scripts are attached. That'll help me see what the problem is
     
  10. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    The collision script is on my player object to detect when it collides with the obstacle object. at the moment i have it set to debug.break. But I want the platform script (so the one above) to detect this collision and stop moving. none of them have triggers. Would I use delegate?
     
  11. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Sorry, I'm not familiar with delegate
     
    Lmaryon likes this.
  12. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    ok, no worries, thanks for your help anyway! :)