CFK
January 19, 2018, 3:13am
1
Want to create ray-casts offset from the game object.
I guessing my code is offset from the world coordinate.
My game play offset problem.
https://imgur.com/tbIcLbr
How to offset it from local coordination? or other way to achieve it?
Vector3 rayPos = new Vector3(transform.position.x, 0, transform.position.z+2);
Vector3 rayPos3 = new Vector3(transform.position.x+2, 0, transform.position.z + 2);
Vector3 rayPos4 = new Vector3(transform.position.x-2, 0, transform.position.z + 2);
Vector3 rayPosBack = new Vector3(transform.position.x, 0, transform.position.z - 2);
Debug.DrawRay(rayPos, transform.forward * 200, Color.red);
Debug.DrawRay(rayPos, transform.right * 200, Color.blue);
Debug.DrawRay(rayPos, transform.right * -200, Color.green);
Debug.DrawRay(rayPos3, transform.forward * 200, Color.yellow);
Debug.DrawRay(rayPos4, transform.forward * 200, Color.cyan);
Debug.DrawRay(rayPosBack, transform.right * 200, Color.black);
Debug.DrawRay(rayPosBack, transform.right * -200, Color.white);
You do this with Transform.TransformPoint .
If this is a super-common thing and you want to be able to freely move the origin positions of the raycasts, you might think about making new GameObjects as children of the primary one, offsetting them however you like locally, and just using those Transforms as the various origin points directly using public Transform references and drag-and-dropping them into the inspector.
CFK
January 19, 2018, 5:47am
3
Thank you, Lysander.
Transform.TransformPoint . works perfectly.
Vector3 rayPos = transform.TransformPoint(Vector3.forward * 2);
Vector3 rayPos3 = transform.TransformPoint(Vector3.right * 2);
Vector3 rayPos4 = transform.TransformPoint(Vector3.right * -2);
Vector3 rayPosBack = transform.TransformPoint(Vector3.forward * -2);
Debug.DrawRay(rayPos, transform.forward * 200, Color.red);
Debug.DrawRay(rayPos, transform.right * 200, Color.blue);
Debug.DrawRay(rayPos, transform.right * -200, Color.green);
Debug.DrawRay(rayPos3, transform.forward * 200, Color.yellow);
Debug.DrawRay(rayPos4, transform.forward * 200, Color.cyan);
Debug.DrawRay(rayPosBack, transform.right * 200, Color.black);
Debug.DrawRay(rayPosBack, transform.right * -200, Color.white);