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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cam Checker.

Discussion in 'Scripting' started by False_Baconator, Jan 1, 2019.

  1. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    Hi, I'm making a 2D Dungeon crawler and I'm trying to make it so that when my ally is off screen, it will teleport to the players position.

    what actually happens, is that the ally teleports to the player and they both start speeding off to the right, regardless of whether or not the ally is off screen.

    here's the relevant code I have. (the code is attached to the camera.)

    Code (CSharp):
    1.  
    2.         GameObject AllyChecker = GameObject.FindGameObjectWithTag("Ally");
    3.  
    4.         if (AllyChecker != null)
    5.         {
    6.             Collider2D[] AllyCamChecker = Physics2D.OverlapBoxAll(CamCenter.position, size, AllyLayer);
    7.  
    8.  
    9.  
    10.             for (int i = 0; i < AllyCamChecker.Length; i++)
    11.             {
    12.                 if(AllyCamChecker[i] == Ally)
    13.                 {
    14.                     AllyOnCamChecker = true;
    15.                 }
    16.             }
    17.  
    18.             AllyOnCam = AllyOnCamChecker;
    19.  
    20.             if (AllyOnCam == false)
    21.             {
    22.                 Ally.transform.position = Player.transform.position;
    23.             }
    24.  
    25.             AllyOnCamChecker = false;
    26.         }
    27.  
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @False_Baconator

    Didn't you have pretty much similar thread already?

    Anyway, it's hard to say, it could be anything, this code doesn't tell anything about your setup.

    If you for some reason move your companion exactly to same spot, I could think your characters might just have colliders.
     
  3. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    i agree, two units with colliders flying off into the sunset sounds like you have them both bouncing off each other. you could use instead of its actually location
    Player.Collider.ClosestPoint(Ally.transform.position)
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DownER149

    I don't think there is ClosestPoint for Physics2D. Haven't used it, but just checked this out of curiosity.
     
  5. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    yeah your right, my mistaken... its bounds.closestpoint(). i used it awhile ago but scrapped it because it would only work when outside of the collider,
     
    eses likes this.
  6. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    so is it "Player.Collider.bounds.closestpoint()"?
     
  7. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    I did have a similar thread already, but i thought id make a new one with an update on what i currently had.
     
  8. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    correct
     
  9. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    its says that GameObject doesn't have a definition for Collider.

    am i missing a using directive? I'm currently using System.Collections, System.Collections.Generic, System.Linq, and UnityEngine.
     
  10. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    in the editor add a boxCollider2D to the player gameobject(if it doesnt already have one)
    if not
    on a script that the player game object has put this
    Code (CSharp):
    1. public BoxCollider2d Collider;
    2.  
    3. Start(){
    4. Collider = getComponent<BoxCollider2d();
    5. }
    6.  
     
  11. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    yeah, its still saying that GameObject Doesnt Have A definition for Collider. I made sure it was public. should i show the whole of my camera script?

    Code (CSharp):
    1. public BoxCollider2D Collider;
    2.  
    3.     void Start () {
    4.         Collider = GetComponent<BoxCollider2D>();
    5.     }
     
    Last edited: Jan 2, 2019
  12. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    im going to unwatch this thread since ive made adjustments that would make this thread obsolete.