- Home /
Isometric Raycast: misplaced origin & not updating position with TransformPoint
Hi guys,
I'm sure I'm missing something simple but I can't get this to work.
I have a player cube in an orthographic view. When the player moves or jumps, I want to cast 4 rays on each vertex on the contrary face to the movement. I'm first testing with one raycast. I have the vertex positions of the collider in local space in a function called Calcpositions().
The problem is: if I transform the local position of one collider vertex with TransformPoint, the position is like half the height of the cube too much up. I don't understand why this is being added.
On the contrary, if I comment the code line that transforms the local position of any vertex to a global position, then the ray is properly casted, but only when I hit play. As soon as I move the cube, the ray will stay in its position but won't follow and update with the movement of the cube.
I've tried adding and subtracting transform.position to both local and global position of any vertex, but the ray is yet misplaced.
I attach a screenshot so you can see how the ray looks like on the Scene viewer (in the Game view it would seem as if the ray has the origin in the proper vertex position -in the example v3FrontTopLeft- but on the scene view it can be seen this is not true).
1) Why the ray does not update its position in local scale I understand, as the vertex positions are always the same ones no matter if the cube player updates its position. How I can do to properly update its movement and follow the player cube transform without using TransformPoint? or that's not possible? (i've tried to add transform.position to the vertex position but it doesn't work as it alters the original coordinates and the vertexs are again misplaced as when using TransformPoint).
2)If I use TransformPoint, why the vertex coordinates are half what they should be?
3) How can I cast the ray from the proper vertex position as it happens when I use the local scale coordinates (but then it does not follow the cube player movement), and at the same time update its position and follow the player cube as when I use TransformPoint or I add the transform.position (but the the ray is misplace too up but half the height of the cube it seems)?
Any hint or help on this is welcome, i've been stuck for a couple of days now.
void Update()
{
transform.Translate(Vector3.down * -gravity * Time.deltaTime);
KeyReleaseHandler();
GetKeyInput();
MovePlayer();
CalcPositons();
}
public void CalcPositons(){
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
Vector3 v3FrontTopLeft;
Vector3 v3FrontTopRight;
Vector3 v3FrontBottomLeft;
Vector3 v3FrontBottomRight;
Vector3 v3BackTopLeft;
Vector3 v3BackTopRight;
Vector3 v3BackBottomLeft;
Vector3 v3BackBottomRight;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
Debug.Log (bounds.center);
Debug.Log (bounds.extents);
v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
v3BackTopRight = transform.TransformPoint(v3BackTopRight);
v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
Debug.DrawRay(v3FrontTopLeft, Vector3.up *1.5f, Color.red);
}
PS. Notice I'm only using Debug.DrawRay for testing purposes without any Physics.Raycast(v3FrontTopLeft, transform.up, out hit, 10f); I will use it once I get this right.
Answer by stalyan77 · Aug 21, 2015 at 10:33 PM
This was finally solved.
I was using Bounds.center, that returns coords in global space, instead of Collider.bounds.center (and extents), that returns coords in local space,. I then used transform.TransformPoint and the rays are properly positioned in global space =)