- Home /
Rotating spine to aim gun toward raycasthit
Hi !
I search for a long time and i cant find a way to rotate the spin character in order to aim the gun toward a raycast hit (created by the cam raycast).
In the picture you can see :
in red : the cam raycast
in green : the spin raycast (which is facing toward a strange direction)
in blue : the gun raycast
in yellow : the wanted gun raycast
This is what i tried for now :
void LateUpdate() {
RaycastHit hit;
if (Physics.Raycast (camera.position, camera.forward, out hit)) {
print ("Raycast detected");
Quaternion lookRotation = Quaternion.LookRotation (hit.point - spin.position);
spin.rotation = lookRotation * initialRotation;
} else {
print("Raycast not detected");
}
}
Thx in advance for your help !
I found a simple way to do that with :
go.LookAt(hit.point);
Go being a gameobject between Child of Spine and Parent of Spine1, the problem is that i loose the prefab of the model and the character doesn't animate the upperbody anymore.
Is there a way to add an object to the model without loosing the prefab connection ?
In fact, this doesn't resolve everything because the gun raycast is not aligned with the cam RayCastHit, only the GO raycast
Okay, it work now but it's not totally accurate :
Quaternion q = Quaternion.FromToRotation(gunShotSource.forward, hit.point - gunShotSource.position);
spin.rotation = q * spin.rotation;
I'm looking for a smooth effect now.
Ok now i'm almost done, but when i try to follow the head i encounter a bug : the camera is shaking when the target is to close.
How can i follow the head accurately without having this bug ?
Any help would be welcome.