Hi, I ask for help because I am trying to indicate with a LookAt a point of a ray that I have placed in front of the transform.
I created this code that works but when the object moves, the point does not move together with the object but remains fixed at the origin point (although I indicate that the origin is the position of the transform).
Ray r = new Ray (transform.position, transform.TransformDirection (Vector3.forward));
var head_ray = r.GetPoint(rayDistance); //raydistance is a float
transform.LookAt(head_ray)
I cannot indicate a point on the ray always in front of the transform. I also tried to use a raycast, but it always gave me the “hit” value 0 (maybe because it didn’t collide with anything, in this case I don’t need it because I always need to find the information on the position indicated by the ray)
The ray is coming out the +Z from where you are.
LookAt orients your +Z towards it.
Your points returned from the ray are all going to be on the same line going out from where you are here.
Therefore your head will never turn.
It’s like you are standing on a straight railroad track watching a train go away. You never have to turn your head to see it.
Try making something ELSE look at a moving point. The above code should work for that.
Also, this:
transform.TransformDirection (Vector3.forward)
can just be writtten as:
transform.forward
, a handy shortcut
Thanks a lot for the answer. I have however seen that using raycast everything is fine when it returns me a hit value. Is there a way to make me always return the point of the end of the ray without having to collide with objects?
Because basically I just want the position of the beam in front of the transform but set in this way the code gives me the position only by colliding.
RaycastHit hit;
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * rayDistance, Color.green);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, rayDistance, layer);
float check= hit.point.y;
Rays don’t have an end. They are a point in space and a direction.
If you know how many world units down the line you can always use .GetPoint()
What exactly are you trying to do? There are also other forms of raycasting, btw:
Raycasting, colliders, planes, Plane, etc:
https://discussions.unity.com/t/801152/2
https://discussions.unity.com/t/828929/4
Yes you just use Ray.GetPoint():
Vector3 point = ray.GetPoint(desiredDistance);
What I would like to do is to start a ray from the head of a body forward (in the direction of the head) and understand by approaching another object, if that is to the right or left of the forward direction of the head.
To do this I need to collect the position in which it looks the head at the same and compare it with that of the target (an example is that I tried to compare target position.x with transform.forward.x but also in the same position X in the world space the two measures have different sizes)
With a radius that gives me a hit.point instead the X axis of the radius is equal in size to that of the target if in its same position and this makes me understand that I am measuring the same space.
I wanted to use GetPoint () as in the example above, to take a point, but it seems to indicate a position of the ray which then does not move together with the ray, but which indicates a fixed point (maybe I’m wrong).