Hi everyone,
I have a turret made of 2 parts:
-the "Base" that can only rotate left/right;
-the "Body" that can only rotate up/down and is a child object of "Base";
-----------------
Both objects have a rotation speed that can be different.
The problems i'm having are:
- if i rotate the **Body** by modifying its '.rotation' directly, which i'd prefer to avoid,the two objects may rotate at different speed ([see this video][1]);
I used this method because i'm not able to lock the rotation on the **Quaternion** in order to rotate '.localRotation' only of the "up/down" amount needed;
- if i rotate using '.localRotation', i use **Vector3**.Lerp to rotate from the current localRotation to the target one,this approach work for fast rotation speed,**but when the speed is too low**, the mesh start rotating like crazy **when horizontal**. It's probably an interpolation error but i don't know how to solve it.
([see this video][2])
-----------------
Any idea on how to solve this problem? (i'd prefer to edit the local rotation since Body is a child of Base)
Thanks in advance.
-----------------
Here's the code i'm using to rotate the Base and the Body.
(**note**: the code commented lines are used for the local rotation instead of the global)
if (Physics.Raycast(camRay, out hitRay, 100f, shootableMask))
{
//Rotate the base of the turret (left/right only)
Vector3 baseToMouse = hitRay.point - b_Base.position;
baseToMouse.y = 0;
Quaternion baseRotation = Quaternion.LookRotation(baseToMouse);
b_Base.rotation = Quaternion.RotateTowards(b_Base.rotation, baseRotation, BaseTurnSpeed *100 *Time.deltaTime);
//Rotate the body of the turret (up/down only)
Vector3 bodyToMouse = hitRay.point - b_Body.position;
//find the rotation but only use X rot
Quaternion bodyRotation = Quaternion.LookRotation(bodyToMouse);
Vector3 eulBody = bodyRotation.eulerAngles;
//save them to reapply,only need to change the local X rotation
Vector3 eulLocalBody = b_Body.localRotation.eulerAngles;
//Vector3 slowedBodyRotation=Vector3.Lerp(eulLocalBody, eulBody, BodyTurnSpeed * Time.deltaTime);
b_Body.rotation = Quaternion.RotateTowards(b_Body.rotation, bodyRotation, BodyTurnSpeed *100* Time.deltaTime);
//b_Body.localRotation = Quaternion.Euler(slowedBodyRotation.x, eulLocalBody.y, eulLocalBody.z);
}
[1]: https://streamable.com/mesio
[2]: https://streamable.com/wb4iv
↧