- Home /
RayCasting shows wrong point
Hi guys,
I am writing a game like diablo, torchlight with some differences. I want thing that my character look towards to mouse position's x and z axis point(where intersect with world, I do not want a screen point or mouse point on 3d space. I want a world point). So I decided to test my code and i set the came ortho and move to up to my objects. In the image, my mouse on the left yellow object but in the scene, ray drawen to wrong point. here is my code; Please help me i am very Confused about that. Thank you all :) (sorry for bad grammar :) )
void Update () {
transform.rotation = Quaternion.LookRotation (worldRay ());
Debug.DrawRay (transform.position, worldRay ());
}
Vector3 worldRay(){
mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast (mouseRay, out hit, 9999);
return new Vector3(hit.point.x,0,hit.point.z);
}
Answer by whydoidoit · Feb 19, 2014 at 03:48 PM
LookRotation and DrawRay take a direction and not a world point.
transform.rotation = Quaternion.LookRotation(worldRay() - transform.position);
Debug.DrawLine(transform.position, worldRay());
Thanks a lot, it worked. But i confused right now , what is the difference between ray and Line drawing. I mean, I will write a simple projectile launcher that launches objects to mouse position. Soi what should i use for that, line or ray? Thank you again ! :))
So in Unity "Line" functions take two points - rays take a direction.
Normally a launcher should orient itself according to the ray based LookRotation - but if you use LookAt then it's a point in the world.
Raycast sends a ray with an origin and a direction, Linecast casts between two points.