Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Draw a line without loosing my line if I touch an Obstacle

Discussion in 'Scripting' started by Shinsuki93, Nov 14, 2017.

  1. Shinsuki93

    Shinsuki93

    Joined:
    Aug 11, 2017
    Posts:
    38
    Hello everyone,

    I have a situation here...

    Actually, I have this...it's stop my line when I touch an Obstacle. You can see this after the 3 raycast (the IF after)
    But I don't want this.. I want changing the color of the line when I touch an Obstacle and not loosing my line, to let the player come back out of Obstacle without what he draw on the Obstacle..
    I'm not clear I think....

    I'm trying to do an Game like Brain Dots.. (I put an Example of what I seeking on upload)

    Here the code Actually

    Code (csharp):
    1.  
    2.  if (listPoint.Count >= 2)
    3.                     {
    4.                         Vector2 point_1 = listPoint[listPoint.Count - 2];
    5.                         Vector2 point_2 = listPoint[listPoint.Count - 1];
    6.  
    7.                         currentColliderObject = new GameObject("Collider");
    8.                         currentColliderObject.transform.position = (point_1 + point_2) / 2;
    9.                         currentColliderObject.transform.right = (point_2 - point_1).normalized;
    10.                         currentColliderObject.transform.SetParent(currentLine.transform);
    11.  
    12.                         currentBoxCollider2D = currentColliderObject.AddComponent<BoxCollider2D>();
    13.                         currentBoxCollider2D.size = new Vector3((point_2 - point_1).magnitude, 0.1f, 0.1f);
    14.                         currentBoxCollider2D.sharedMaterial = slipperyMaterial;
    15.                         currentBoxCollider2D.enabled = false;
    16.  
    17.                         Vector2 rayDirection = currentColliderObject.transform.TransformDirection(Vector2.right);
    18.  
    19.                         Vector2 pointDir = currentColliderObject.transform.TransformDirection(Vector2.up);
    20.  
    21.                         Vector2 rayPoint_1 = (Vector2)currentColliderObject.transform.position + (-rayDirection) * (currentBoxCollider2D.size.x);
    22.  
    23.                         Vector2 rayPoint_2 = ((Vector2)currentColliderObject.transform.position + pointDir * (currentBoxCollider2D.size.y / 2f))
    24.                                              + ((-rayDirection) * (currentBoxCollider2D.size.x));
    25.  
    26.                         Vector2 rayPoint_3 = ((Vector2)currentColliderObject.transform.position + (-pointDir) * (currentBoxCollider2D.size.y / 2f))
    27.                                              + ((-rayDirection) * (currentBoxCollider2D.size.x));
    28.  
    29.                         float rayLength = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - rayPoint_1).magnitude;
    30.  
    31.                         RaycastHit2D hit_1 = Physics2D.Raycast(rayPoint_1, rayDirection, rayLength);
    32.                         RaycastHit2D hit_2 = Physics2D.Raycast(rayPoint_2, rayDirection, rayLength);
    33.                         RaycastHit2D hit_3 = Physics2D.Raycast(rayPoint_3, rayDirection, rayLength);
    34.  
    35.                        
    36.                           if (hit_1.collider != null || hit_2.collider != null || hit_3.collider != null)
    37.                         {
    38.                             GameObject hit = (hit_1.collider != null) ? (hit_1.collider.gameObject) :
    39.                                             ((hit_2.collider != null) ? (hit_2.collider.gameObject) : (hit_3.collider.gameObject));
    40.                             if (currentColliderObject.transform.parent != hit.transform.parent)
    41.                             {
    42.                                 Destroy(currentBoxCollider2D.gameObject);
    43.  
    44.                                 if (pinkBallRigid.isKinematic)
    45.                                     pinkBallRigid.isKinematic = false;
    46.                                 if (blueBallRigid.isKinematic)
    47.                                     blueBallRigid.isKinematic = false;
    48.  
    49.                                 for (int i = 0; i < currentLine.transform.childCount; i++)
    50.                                 {
    51.                                     currentLine.transform.GetChild(i).GetComponent<BoxCollider2D>().enabled = true;
    52.                                 }
    53.  
    54.                                 if (mode == Mode.LevelEditorMode)
    55.                                 {
    56.                                     levelManager.listLineRendererPos = listPoint;
    57.                                 }
    58.  
    59.                                 listLine.Add(currentLine);
    60.                                 currentLine.AddComponent<Rigidbody2D>().useAutoMass = true;
    61.                                 foreach (Rigidbody2D rigid in listObstacleNonKinematic)
    62.                                 {
    63.                                     rigid.isKinematic = false;
    64.                                 }
    65.                                 stopHolding = true;
    66.                             }
    67.                         }
    68.  

    Thanks you very much for your time and help.

    Best regards,
    Shinsuki
     

    Attached Files: