Question by
Obleynix · Aug 09, 2019 at 01:56 AM ·
unity 5bug-perhapsphysics.raycasttransform.forward
transform.forward not pointing to the blue axis with raycast
I started working with some raycast today, and while doing some tests to get the position where the ray was pointing to. Well, the thing is, i am using transform.forward to find the forward of a cube, but instead it returns to me in a draw ray a position totally diferent. my code:
public class Raycasters : MonoBehaviour
{
public RaycastHit hit;
public GameObject instant;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Debug.DrawRay(transform.position, transform.forward * 10, Color.green); this works fine it points forwards just right
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.transform.gameObject != null)
{
Debug.DrawRay(transform.position,hit.point,Color.yellow); //transform.forward works but the hit point doesnt
//Instantiate(instant,hit.point,Quaternion.identity); i tried instantiating to see if it was just a gizmos bug not the case
}
}
}
}
ribit.jpg
(77.8 kB)
Comment
Try passing a direction vector, ins$$anonymous$$d of a position vector.
Debug.DrawRay(transform.position,hit.point,Color.yellow);
to
Debug.DrawRay(transform.position,(hit.point - transform.position),Color.yellow);
Answer by himanshugupta159 · Aug 21, 2019 at 11:07 AM
Hello, i have written a blog Describing every bit of detail regarding Physics Raycast.Please have a look and I am sure it will solve your Doubts.
https://unfragilecoding.blogspot.com/2019/08/physicsraycast-unity.html