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

**SOLVED* Keep Raycasts aligned with object and transforms, animations etc? Unity 5 0.0.0f4

Discussion in 'Physics' started by NicholasGolden, Mar 11, 2015.

  1. NicholasGolden

    NicholasGolden

    Joined:
    Jul 11, 2013
    Posts:
    10
    Hi,

    I'm making a Table simulator 2015 and need some help to get the rayCasting to work correctly. I'm in UNITY 5 0.0.0f4.

    I have a cube (the table top) and I need collision detection on 4 corners (the legs of the table). The rays currently align to the 4 corner faces of the cube, I got some help with that it was a little trickier than I thought.

    The issue:

    If you look at these two screenshots you'll see that the first one shows the cube and the rays, nice and where I want them, and if I were to rotate the cube in any direction they should come along for the ride and stay aligned.


    In this shot - the rayCasts are aligned with my cube

    Aligned Rays.png


    In this screenshot, I apply a 45 value to the Y transform, as you can see the rays are staying in the original spot where the cube face was, but did rotate (I guess that was nice of them).
    Not Aligned Rays.png


    Thanks in advance for any advice or code etc. I know it's something like LocalPosition or something like that and I've been searching like crazy, found some other stuff that wasn't close to what I wanted. I've heard "stick it in fixed update" but I don't think that's the core of the issue. I'm basically trying to say the player position = raycast position and should stay with it regardless of orientation. I just can't seem to figure it out, and the unity docs I got this working, but still don't know how to progress :)

    Thanks again!
    -Nick


    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassAnotherRayCast : MonoBehaviour {
    6.  
    7. publicfloatRayLength = 10; //lengthoftheray, usesliderininspector
    8.  
    9.  
    10. publicfloatPlayerXWidth = 3;
    11. publicfloatPlayerYHeight = 3;
    12. publicfloatPlayerZLength = -10;
    13.  
    14. RaycastHit_hit;
    15.  
    16. boolshouldRayCast = true;
    17.  
    18.  
    19. voidLateUpdate()
    20. {
    21. Update ();
    22. }
    23.  
    24.  
    25. voidUpdate() {
    26.  
    27.  
    28.  
    29. //Definethevectors
    30. Vector3up = transform.TransformDirection (Vector3.up) * RayLength;
    31. Vector3down = transform.TransformDirection (Vector3.down) * RayLength;
    32. Vector3left = transform.TransformDirection (Vector3.left) * RayLength;
    33. Vector3right = transform.TransformDirection (Vector3.right) * RayLength;
    34. Vector3fwd = transform.TransformDirection (Vector3.forward) * RayLength;
    35. Vector3back = transform.TransformDirection (Vector3.back) * RayLength;
    36.  
    37. //NowletsoffsettheraycastsanddrawadebugraysoIcanseehowthingslineup
    38. Vector3Fwd_TopLeftFront = transform.position + newVector3(PlayerXWidth / 2f, PlayerYHeight / 2f, PlayerZLength / 2f);
    39. Debug.DrawRay(Fwd_TopLeftFront, fwd, Color.yellow);
    40.  
    41. Vector3Fwd_TopRightFront = transform.position + newVector3(PlayerXWidth / -2f, PlayerYHeight / 2f, PlayerZLength / 2f);
    42. Debug.DrawRay(Fwd_TopRightFront, fwd, Color.magenta);
    43.  
    44. Vector3Fwd_LowerLeftFront = transform.position + newVector3(PlayerXWidth / 2f, PlayerYHeight / -2f, PlayerZLength / 2f);
    45. Debug.DrawRay(Fwd_LowerLeftFront, fwd, Color.green);
    46.  
    47. Vector3Fwd_LowerRightFront = transform.position + newVector3(PlayerXWidth / -2f, PlayerYHeight / -2f, PlayerZLength / 2f);
    48. Debug.DrawRay(Fwd_LowerRightFront, fwd, Color.black);
    49.  
    50.  
    51.  
    52. //usedforlateron - shouldRayCastwillbechecked, andonlyasinglehitwillberegisteredbeforeIkillthegamestate
    53. //notimplemented, butitworks, justcommentedout.
    54. if (shouldRayCast && Physics.Raycast(Fwd_TopLeftFront, fwd, out_hit, RayLength))
    55.  
    56.  
    57. if (_hit.collider.tag == "Obstacle")
    58. {
    59. print("Fwd_TopLeftFront hit something");
    60.  
    61.  
    62. if (_hit.collider != null)
    63. {
    64. //shouldRayCast = false;
    65. }
    66.  
    67. }
    68.  
    69. if (shouldRayCast && Physics.Raycast(Fwd_TopRightFront, fwd, out_hit, RayLength))
    70.  
    71.  
    72. if (_hit.collider.tag == "Obstacle")
    73. {
    74. print("Fwd_TopRightFront hit something");
    75.  
    76.  
    77. if (_hit.collider != null)
    78. {
    79. //shouldRayCast = false;
    80. }
    81.  
    82. }
    83.  
    84. if (shouldRayCast && Physics.Raycast(Fwd_LowerLeftFront, fwd, out_hit, RayLength))
    85.  
    86.  
    87. if (_hit.collider.tag == "Obstacle")
    88. {
    89. print("Fwd_LowerLeftFront hit something");
    90.  
    91.  
    92. if (_hit.collider != null)
    93. {
    94. //shouldRayCast = false;
    95. }
    96.  
    97. }
    98.  
    99. if (shouldRayCast && Physics.Raycast(Fwd_LowerRightFront, fwd, out_hit, RayLength))
    100.  
    101.  
    102. if (_hit.collider.tag == "Obstacle")
    103. {
    104. print("Fwd_LowerRightFront hit something");
    105.  
    106.  
    107. if (_hit.collider != null)
    108. {
    109. //shouldRayCast = false;
    110. }
    111.  
    112. }
    113.  
    114. }
    115. }
    116.  
     
    Last edited: Mar 11, 2015
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    One way to solve:
    - Add empty gameobjects (as child to the box) on those corners and use them as reference (starting points for the rays)

    Or, otherwise need to fix those corner position calculations..(they dont take account the rotation)..

    Also you can get gameobject forward pointing direction with:
    http://docs.unity3d.com/ScriptReference/Transform-forward.html
    (instead of doing transform directions)
     
  3. NicholasGolden

    NicholasGolden

    Joined:
    Jul 11, 2013
    Posts:
    10
    Hey mgear,

    Sounds like first suggestion isn't optimal, or maybe it is hah. I'll take a look at that and second suggestion (looked at docs that second example on link looks promising) when kiddies get to bed :)

    Thanks for the reply, I'll let you know! -nick
     
  4. NicholasGolden

    NicholasGolden

    Joined:
    Jul 11, 2013
    Posts:
    10
    Ok, so your suggestion - empty gameObjects worked. Here is the updated code, for anyone else that runs across this silly problem.

    Also a guy named Whitaker Trebella suggested same thing with empty game objects, and also to add in publicGameObjectcorner1; so I could access those empty GO's in the inspector.

    Whitaker made Polymer and Pivvot on iOS (Pivvot is also on Steam).

    Figured at least I could shout out to him since he helps me out a lot :)


    So, thank you both!

    -Nick


    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassAnotherRayCast : MonoBehaviour {
    6. publicGameObjectcorner1;
    7. publicGameObjectcorner2;
    8. publicGameObjectcorner3;
    9. publicGameObjectcorner4;
    10.  
    11. publicfloatRayLength = 10; //Lengthoftheray
    12.  
    13. voidUpdate() {
    14.  
    15. Vector3fwd = transform.TransformDirection (Vector3.forward) * RayLength;
    16.  
    17. Vector3Fwd_TopLeftFront = corner1.transform.position;
    18. Debug.DrawRay(Fwd_TopLeftFront, fwd, Color.yellow);
    19.  
    20. Vector3Fwd_TopRightFront = corner2.transform.position;
    21. Debug.DrawRay(Fwd_TopRightFront, fwd, Color.magenta);
    22.  
    23. Vector3Fwd_LowerLeftFront = corner3.transform.position;
    24. Debug.DrawRay(Fwd_LowerLeftFront, fwd, Color.green);
    25.  
    26. Vector3Fwd_LowerRightFront = corner4.transform.position;
    27. Debug.DrawRay(Fwd_LowerRightFront, fwd, Color.black);
    28.  
    29.  
    30. }
    31. }
    32.  

    Also, if someone wanted to see it rotate around in inspector without manually sliding transforms around you can use this script, which came from somewhere I have no idea where or else I'd credit it :)

    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassObjectRotate : MonoBehaviour {
    6.  
    7. publicfloatmyRotationSpeed = 100.0f;
    8.  
    9. publicboolisRotateX = false;
    10. publicboolisRotateY = false;
    11. publicboolisRotateZ = false;
    12.  
    13. //CHANGETOROTATEINOPPOSITEDIRECTION
    14. privateboolpositiveRotation = false;
    15.  
    16. privateintposOrNeg = 1;
    17.  
    18. //Usethisforinitialization
    19. voidStart ()
    20. {
    21. GetComponent<Collider>().isTrigger = true;
    22. if(positiveRotation == false)
    23. {
    24. posOrNeg = -1;
    25. }
    26. }
    27.  
    28. //Updateiscalledonceperframe
    29. voidUpdate ()
    30. {
    31. //TogglesXRotation
    32. if(isRotateX)
    33. {
    34. transform.Rotate(myRotationSpeed * Time.deltaTime * posOrNeg, 0, 0);//rotatescoinonXaxis
    35. //Debug.Log("YouarerotatingontheXaxis");
    36. }
    37. //TogglesYRotation
    38. if(isRotateY)
    39. {
    40. transform.Rotate(0, myRotationSpeed * Time.deltaTime * -posOrNeg, 0);//rotatescoinonYaxis
    41. //Debug.Log("YouarerotatingontheYaxis");
    42. }
    43. //TogglesZRotation
    44. if(isRotateZ)
    45. {
    46. transform.Rotate(0, 0, myRotationSpeed * Time.deltaTime * posOrNeg);//rotatescoinonZaxis
    47. //Debug.Log("YouarerotatingontheZaxis");
    48. }
    49.  
    50. }
    51.  
    52. }
    53.  
    54.  
    Finally, you can see the end result.

    Cube with rays, at one angle
    Screen Shot 2015-03-12 at 9.42.57 AM.png

    Then cube with rays, turned a bit to show that the rays stayed with it

    Screen Shot 2015-03-12 at 9.43.09 AM.png