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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Help with boxcast and visualization?

Discussion in 'Physics' started by Xerun1, Oct 24, 2022.

  1. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    I've been looking into using a boxcast and its the first time I've used one. I am trying to create a box with center 1 X unit in front of the player , with dimensions 3f ->X, 1f -> Y, 1f -> Z so that it forms a rectangle.

    This is the code I've used and it seems to be very wrong. But I can't understand why:

    Code (CSharp):
    1. mHit = Physics.BoxCast(transform.position + transform.forward,new Vector3(1.5f,.48f,.5f), transform.position,out mHitDetect,Quaternion.identity,1f);
    2.  
    3.  
    4. Gizmos.DrawWireCube(transform.position + transform.forward * mHitDetect.distance, new Vector3(3f,.8f,1f));
    5.            
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Your boxcast arguments are all wrong:

    Code (CSharp):
    1. mHit = Physics.BoxCast(transform.position + transform.forward,new Vector3(1.5f,.48f,.5f), transform.position,out mHitDetect,Quaternion.identity,1f);
    2.  
    According to https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html it's:
    public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation = Quaternion.identity, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);


    So you've got:
    transform.position + transform.forward
    for the center, which makes no sense. It should probably just be
    transform.position
    .
    You've got
    transform.position
    for the direction, which makes no sense. It should probably be
    transform.forward
     
  3. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    I think my understanding of Transform.Forward is wrong then. The way I understood it was that transform.forward represents 1 unit infront of the vector you are facing. So of you're facing at rotation (0,0,0) then the transform.forward is (1,0,0). I imagine that is not correct. What then does transform.forward represent? Does it have a value


    I think I got confused as well with Boxcasting. I know think what is happening is that the box is generating and then moving out at a distance. Which is not what I was after. I realized later that I was after an OverlapBox rather than a boxcast.

    My understanding now is that if I rotate my character and use the following this will always work?
    Physics.OverlapBox(transform.position + transform.forward, new Vector3(size of box based on rotation), Quaternion.identity);
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    transform.forward is a direction vector. It is the direction your object is facing. The position of the object is irrelevant for transform.forward.

    If your object is facing along the world forward(z) axis, then it's (0, 0, 1).
    If your object is rotated 180 degrees on the y axis from there, it will be (0, 0, -1).

    This will work to check a box that is centered 1 unit in front of the character. I'm not sure what you mean by "size of box based on rotation" though. Why would the size of the box be based on the rotation?
     
  5. Xerun1

    Xerun1

    Joined:
    Sep 9, 2022
    Posts:
    15
    Ok good. Then I do understand it. Yes I want my box to check one unit in front of the character.

    as for Size of Box based on rotation, apologies I should have made my meaning clearer, thats entirely my bad.

    What I mean is the size is of type Vector3, so if I am rotated 90 or 270 degrees, then the box will be length wise towards the character instead of what I was aiming for. so i switch the X/Z values based on the rotation of the character. Is there a better way to do that?

    Otherwise thank you so much for your assistance. I read through the Unity documentation but I find it to be a bit confusing so didnt really understand it 100%
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    As per the docs (https://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html): OverlapBox has a
    Quaternion orientation
    parameter. This is how you define how the box is rotated. You would not modify the size for this purpose.