Hello, I have a gameObject that is a rectangle and im raycasting around it to check if it hits something. It works great when the object has 0 rotation. As soon as the rotation changes then the raycasts dont work because the math involed to calculate the raycasting spots are wrong. I was wondering if there is a way to rotate the gameObject after the raycasts have been set, so I can get the effect that I want.
The gameObject is inside a Canvas so im working in 2D screen space.
Any help would be much appreciated!
The code in C# that I use:
public bool RaycastCheck()
{
var size =
new Vector2(_collider.size.x * Mathf.Abs(transform.localScale.x),
_collider.size.y * Mathf.Abs(transform.localScale.y)) / 2;
var center = new Vector2(_collider.center.x * transform.localScale.x, _collider.center.y * transform.localScale.y);
Vector3 _raycastTopLeft = transform.position + new Vector3(center.x - size.x, center.y + size.y);
Vector3 _raycastBottomRight = transform.position + new Vector3(center.x + size.x, center.y - size.y);
Vector3 _raycastBottomLeft = transform.position + new Vector3(center.x - size.x, center.y - size.y);
Debug.Log("topleft"+_raycastTopLeft);
Debug.Log("bottomleft" + _raycastBottomLeft);
Debug.Log("bottomeright" + _raycastBottomRight);
float verticalDistaceBetweenRays = (_collider.size.x * Mathf.Abs(transform.localScale.x)) / 9;
float horizontalDistanceBetweenRays = (_collider.size.y * Mathf.Abs(transform.localScale.y)) / 3;
Vector2 transformUp = GetComponent<RectTransform>().rotation * Vector2.up;
Vector2 transformDown = GetComponent<RectTransform>().rotation * -Vector2.up;
Vector2 transformRight = GetComponent<RectTransform>().rotation * Vector2.right;
Vector2 transformLeft = GetComponent<RectTransform>().rotation * -Vector2.right;
for (int i = 0; i < 10; i++)
{
var rayVector = new Vector2(_raycastTopLeft.x + (i * verticalDistaceBetweenRays), _raycastTopLeft.y);
var rayVector1 = new Vector2(_raycastBottomLeft.x + (i * verticalDistaceBetweenRays), _raycastBottomLeft.y);
Debug.DrawRay(rayVector, transformUp * 3.5f, Color.red);
Debug.DrawRay(rayVector1, transformDown * 3.5f, Color.red);
var rayCastHit = Physics2D.Raycast(rayVector, transformUp, 3.5f);
var rayCastHit1 = Physics2D.Raycast(rayVector1, transformDown, 3.5f);
if (rayCastHit || rayCastHit1)
{
//Debug.Log("HIT");
return false;
}
}
for (int i = 0; i < 4; i++)
{
var rayVector = new Vector2(_raycastBottomLeft.x, _raycastBottomLeft.y + (i * horizontalDistanceBetweenRays));
var rayVector1 = new Vector2(_raycastBottomRight.x, _raycastBottomRight.y + (i * horizontalDistanceBetweenRays));
Debug.DrawRay(rayVector, transformLeft * 3.5f, Color.red);
Debug.DrawRay(rayVector1, transformRight * 3.5f, Color.red);
var rayCastHit = Physics2D.Raycast(rayVector, transformLeft, 3.5f);
var rayCastHit1 = Physics2D.Raycast(rayVector1, transformRight, 3.5f);
if ((rayCastHit.collider != null) || (rayCastHit1.collider != null))
{
//Debug.Log("HIT");
return false;
}
}
return true;
}
Here is a visualization of what I want:
