- Home /
Rotating Raycast with object??
Hello,
I'll start off by describing my problem: I have a cube that I want to rotate along the Y-axis. I use transform.RotateAround for this. I also want to shoot a raycast forward from the corner (max) of the cube's collider's bounds (col.bounds.max). Now I know that col.bounds.max is not a local coordinate, however I've also tried to use mesh.bounds.max and renderer.bounds.max and have the same issue below:
After I rotate the cube around the Y-axis the Ray separates from it's origin (bounds.max):
This is my code:
public float rotSpeed = 6f;
BoxCollider col;
Renderer rend;
void Start() {
col = GetComponent<BoxCollider>();
rend = GetComponent<Renderer>();
}
void Update() {
float inputX = Input.GetAxis("Horizontal");
transform.RotateAround(rend.bounds.center, transform.up, inputX * rotSpeed * Time.deltaTime);
Debug.DrawRay(rend.bounds.max, transform.forward, Color.green);
}
Like I said, i've tried col.bounds, mesh.bounds and renderer.bounds to no avail. I need to be able to rotate that Ray with the cube without it disconnecting like that and the only way that seems to work is if I use col.bounds.center which will not work for my purposes.
Thanks in advance, Colin
What you are getting is an axis-aligned bounding box. Bounds API Reference This is the smallest imaginary box you could draw around your object in 3D space, whose edges are parallel to the Cartesian coordinate system. These are used (mostly internally) for efficiently sorting objects in 3D space.
It's not really clear what you're trying to do. It seems like what you are asking for would only be useful in a very specific circumstance - you'll only ever find one specific corner by taking the max of the kind of bounding box you actually want - which makes me think what you actually want needs a better explanation.
Can you elaborate further? $$anonymous$$aybe we can find a solution that works for you.
Answer by Rabee_Kiblawi · Jul 21, 2017 at 08:07 PM
hello I know its late for reply ...but I think other people will benefit from this ... ok I struggled a lot for raycasting from rotation , the actual problem is giving an angle to the Vector3 ...there is Vector3.up , Vector3.left...yet there is no Vector3.angle() ...so what I basically did is finding a relation for the x,z values upon the rotation of the y axis using (euler angles) ...my result was x=cos(y) and z = -sin(y) ...
float x_offset = (Mathf.Cos(Mathf.Deg2Rad * (my_transform.eulerAngles.y)));
float z_offset =(-Mathf.Sin(Mathf.Deg2Rad * (my_transform.eulerAngles.y)));
bool myRay = Physics.Raycast(toEdge_origin.position, new Vector3(x_offset,0,z_offset), out edge_hit, 1.5f);
you could also do that for the x and z rotation by the same method (cos and -sin) @colinter191