- Home /
[HELP]Object Look At Raycast position
Hi guys, I'm here another time with another problem that i can't solve Ok here is the code
var hit : RaycastHit;
var direction = CameraTarget.transform.TransformDirection(Vector3.forward);
if(CameraTarget){
var rotationY = Quaternion.EulerRotation(Vector3(0,Physics.Raycast(CameraTarget.transform.position, direction, hit),0));
var rotationX = Quaternion.EulerRotation(Vector3(Physics.Raycast(CameraTarget.transform.position, direction, hit),0,0));
Misc_a.transform.localRotation = rotationY;
Misc_b.transform.localRotation = rotationX;
}
Ok now i'll explain. I have a 3D model of a minigun Platform(like the one on the blackhawk helicopter), now the model if made with 4 parts: 1. The base of the minigun 2. The Misc_a object 3. The Misc_b object 4. The Misc_c object The base doesn't move or rotate, the Misc_a must rotate along its own Y axis looking at the Raycast Point (that has the origin from an object above the Main Camera that cast a ray), the misc_b must rotate along its own X axis looking at the Raycast Point, but in my script there are 2 errors related at the Vector3 in the Quaternion.EulerRotation. Can anyone tell me how to do what i've writed above?
Thanks Bye
Answer by Berenger · Jun 04, 2012 at 03:23 PM
Physics.Raycast returns a boolean, not a number. If you want the point the ray hits, it's hit.point.
For the rotation, you can calculate the vector gun -> point, set the y = 0 for Misc_a and x = 0 for Misc_b and affect it to the respective transform.forward.
Answer by GutoThomas · Jun 04, 2012 at 03:29 PM
Maybe:
var hit : RaycastHit;
var direction = CameraTarget.transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(CameraTarget.transform.position, direction, hit){
Misc_a.rotation = Quaternion.LookRotation(hit.point);
Misc_a.eulerAngles = Vector3(0, Misc_a.eulerAngles.y, 0); // reset the rotation in x and z axis
Misc_b.rotation = Quaternion.LookRotation(hit.point);
Misc_b.eulerAngles = Vector3(Misc_b.eulerAngles.x, 0, 0); // reset the rotation in y and z axis
}
Firstly, you need to launch the raycast to store the position where it collided with something. Then, you'll be able to play with this collision point as you want.
(I assumed your answer was a comment to José's answer.)
Giacomo : it gives me 2 errors about $$anonymous$$isc_a.rotation = Quaternion.LookRotation(Vector3(hit.point)); $$anonymous$$isc_b.rotation = Quaternion.LookRotation(Vector3(hit.point));
it tells: The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(UnityEngine.Vector3)'.
It gives me 2 errors: The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(UnityEngine.Vector3)'.
the lines are: $$anonymous$$isc_a.rotation = Quaternion.LookRotation(Vector3(hit.point)); $$anonymous$$isc_b.rotation = Quaternion.LookRotation(Vector3(hit.point));
maybe the Vector3 must have 3 value, not only one?
Sorry, it's obviously not needed to call the constructor, since hit.point is already a Vector3. $$anonymous$$y bad.
just:
$$anonymous$$isc_a.rotation = Quaternion.LookRotation(hit.point);
ehm it's still not working, damn the $$anonymous$$igun $$anonymous$$isc_b doesn't follow the $$anonymous$$isc_a movement and these 2 parts point an empty space in the game scene, i don't know why. Any suggestion? And thanks for scripts and comments.