Obtain a Transform using RayCastAll without a Collision (ie a point in space)
I need to get a transform from the camera when there is no collision (ie. just say 100 units in front of the camera) I already have raycasting and collison detection working but for this i am stuck. I am trying to use RayCastAll but it appears to return nothing. Any ideas?
Note: the Debug.DrawRay() works as it should, how do i get the transform of the end point?
Debug.DrawRay(camPos, direction);
RaycastHit[] lookAt = Physics.RaycastAll(camPos, direction, 100); // RayCastAll to get a position in space at 100 unit infront of the ray
if (lookAt.Length != 0) hit3D = lookAt[0].transform; // We only care about the first element
else Debug.Log("lookAt Array is zero length");
EDIT: I've worked out that the 100 distance is actually working, basically no collision if you are looking say into the sky. So RayCastAll is working for me. So i think i am getting there, however if i do not hit a target and RayCastAll returns nothing, how can i still get the end point.. excuse my confusion if i'm way off on understanding this right.
EDIT2: So it appears that the return value is in local space... how do i get this into world space position?
EDIT3: It would appear that my problem is that what i am trying to do with this transform for a Unity Pro only version, i am talking about The MecanimTute with the laser eyes, Just Get FinalIK from the asset store and be done with it? what do you think? (i Trust it can do this easily)
To get a point in space 100 units from your camera in the direction of the ray is just some vector math:
Vector3 point= camPos + (direction.normalized * 100);
If these values (camPos, and direction) are in the camera's model space, that would be pretty odd, but you can convert the final point to worldspace like so...
Vector3 world_space_point = camera.transform.TransfrormPoint(local_space_point);
Answer by Ivan Kukucov · Sep 29, 2015 at 09:11 PM
So if the name of your RaycasstHit is lookAt
try lookAt.GetPoint(100f); that will give you the point at 100 units and just change the number in the bracets to change the distance of the point.
There is nothing in the editor that is locked for the free version I think.
Answer by ZenMicro · Sep 30, 2015 at 01:23 AM
Hi Guys, Thanks for your answers, I am finding that even in the demo MecanimTut that the head look does not work as it does in the video, the lasers work but no independent head movement and I've heard others mention they thought this feature was in pro only.
Since Unity 5 everything in the editor and what you can do with it is the same in the free and pro version.
I got what i wanted to work thanks. What I was trying to do was a simple headlook, and yeah there are a few ways to get point in space, I went with this;
void OnAnimatorI$$anonymous$$(int LayerIndex)
{
// Headlook
m_LookTarget = m_Cam.transform.forward.normalized * 15;
m_LookWeight = $$anonymous$$athf.Lerp(m_LookWeight, 1f, Time.deltaTime * m_LookSmoother);
anim.SetLookAtPosition(m_LookTarget);
anim.SetLookAtWeight(m_LookWeight);
}
Your answer
Follow this Question
Related Questions
Get tail end of armature bone with script 1 Answer
World Canvas Raycasting not blocked by other game objects? 2 Answers
When hitting border teleport bullet to player. 1 Answer
Add force forward to a ball using local coordinates doesn't work as I wanted 0 Answers
Does it matter what object's transform the TransformPoint() method is attached to? 0 Answers