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

Resolved How to get a face on given direction of a cube

Discussion in 'Scripting' started by RafaelGomes00, Oct 2, 2023.

  1. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    For me to explain my problem in a easy way to understand, I have to explain the mechanic as a whole:

    So, I'm developing a game, where the player can fit pieces with holes in other pieces of diferent sizes, and I'm releasing raycasts to know where the piece should go.
    With that said, Here's the problem:

    - I need to get the upper piece aligned to the bottom piece, you can see that on the bottom hole of the upper piece, there is a green gizmos sphere, that is where my piece is going to, but i needed it to go to the middle of the bottom piece.
    upload_2023-10-2_15-34-52.png

    - I need it to be like this for example:
    upload_2023-10-2_15-36-54.png

    How far the upper piece will fit on the bottom piece is being controled by a raycast that travels trough the lower piece.
    I'm struggling basically with Vectors and Positions, my main logic consist in getting a point in the lower piece, and creating an extended point on a given direction (on that example, the extended point will go up), and I use Bounds.ClosestPoint.
    Here's the piece of code that is responsible for doing this controll:

    Code (CSharp):
    1.     public static Vector3 GetExtendedPoint(Vector3 desiredDirection, RaycastHit hit, Collider targetCollider)
    2.     {
    3.         Vector3 extendedPoint = hit.point + desiredDirection * 1;
    4.         Vector3 extendedPointDirection = targetCollider.transform.forward.normalized;
    5.         extendedPoint = extendedPoint + extendedPointDirection * extendedPoint.y;
    6.  
    7.         Vector3 desiredPosition = targetCollider.bounds.ClosestPoint(extendedPoint);
    8.  
    9.         return desiredPosition;
    10.     }
    I know that the problem is on this line:
    Code (CSharp):
    1. extendedPoint = extendedPoint + extendedPointDirection * extendedPoint.y;
    But I can't figure out a way to fix this, any help will be much appreciated.
    Thanks in advice! :)
     
  2. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    Problem might be because of rotation.
    I would do triggerenter instead of raycast take part you want to place create empty gameobject make it child of the part set transfrom to zero remove the part now you can set exact location you just placed when ontriggerenter set parts transfrom to empty gameobject transfrom.
     
    Last edited: Oct 2, 2023
  3. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    I think I understand your idea, but I do raycasts because of the drag of the object, and I use raycasts to know to which piece I should fit in, if I don't used raycats, the game would be too hard to play.

    And Something I should add as information is that I only snap the moving piece before I do all the calculations of positions.
     
  4. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    164
    You could create empty gameobjects to mark the snapping points of each piece, then compare their position and angles and adjust the position of the piece so they match together when near. No need of expensive raycasts.
     
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Since you have a RaycastHit from your raycast, you can get the surface normal where the ray has hit. Whether the ray has hit where you intended to however, is another matter.
     
  6. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    I managed to fix it!
    Basically, I got the normal that my raycast was pointing to, and did a Vector3.Scale() between the collider.bounds.size and the normal, this gave me what where the size of the direction that I was hitting, and subtracting this with the previous point that I already had returned me the exact point that I needed.

    Code (CSharp):
    1. public static Vector3 GetExtendedPoint(Vector3 desiredDirection, RaycastHit hit, Collider targetCollider)
    2.     {
    3.         Vector3 extendedPoint = hit.point + (desiredDirection * 1) - (Vector3.Scale(targetCollider.bounds.size, hit.normal) / 2);
    4.         Vector3 desiredPosition = targetCollider.bounds.ClosestPoint(extendedPoint);
    5.  
    6.         return desiredPosition;
    7.     }