- Home /
Rotate raycast origin with gameobject
Hi everybody,
I am developing a 2D raycast collision detection where i raycast some rays below my character, so i can see if there is anything below me, and cast some raycasts in the directions that i am moving. Here is my inital raycasts
raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2(bounds.max.x,bounds.min.y);
raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);
To instantiate my raycasts i calculate the space, based on the amount of raycast that i have and the distance between the size of my object (if i want to cast below my character i use bottomLeft and bottomRight to calculate the distance, for example).
My problem is when i rotate my character. The direction of raycasts are rotating, but the origin is not, since i am using my bounds to set the origin it doesn't work when my object is with some rotation. Here is what i used to cast my vertical rays.
for (int i = 0; i < verticalRayCount; i++){
Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft:raycastOrigins.topLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, transform.up *directionY, rayLength, whatIsGround);
Debug.DrawRay(rayOrigin, transform.up * directionY * rayLength,Color.red);
And after this i check some informations about the hit.
When i rotate i get something like this:
Is there a way where i can rotate my origin vector based on the object rotation? I really want to continue using bounds to cast my rays.
Thank you all!
Your answer
![](https://koobas.hobune.stream/wayback/20220613200443im_/https://answers.unity.com/themes/thub/images/avi.jpg)