- Home /
Banking GameObject in Local Space?
I've crafted the following code to point a GameObject in the direction of travel but also bank the GameObject based on its velocity along a specific axis. I have two lines of code, one to pitch on X and one to roll on Z axis. This works however the banking occurs in world space and not in local space. Can this be adapted to work in local space, is there a better way, or can it be optimized somehow?
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookAtTarget) * Quaternion.AngleAxis(Mathf.Clamp(-enemyVelocity.x * xBankSpeed, -xBankRange, xBankRange), Vector3(0,0,1)), bankSmoothing * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookAtTarget) * Quaternion.AngleAxis(Mathf.Clamp(enemyVelocity.z * zBankSpeed, -zBankRange, zBankRange), Vector3(1,0,0)), bankSmoothing * Time.deltaTime);
Thanks.
$$anonymous$$y comment was just plain WRONG as $$anonymous$$accabe pointed out, I have deleted it.
transform.localRotation is the local rotation while transform.rotation is the global rotation.
Also it seems like ins$$anonymous$$d of rotating towards
Quaternion.LookRotation(lookAtTarget) * Quaternion.AngleAxis($$anonymous$$athf.Clamp(-enemyVelocity.x * xBankSpeed, -xBankRange, xBankRange), Vector3(0,0,1))
then towards
Quaternion.LookRotation(lookAtTarget) * Quaternion.AngleAxis($$anonymous$$athf.Clamp(enemyVelocity.z * zBankSpeed, -zBankRange, zBankRange), Vector3(1,0,0))
It might be better to rotate towards one rotation that is some combination of the pitch and roll. For instance something like
Quaternion.LookRotation(lookAtTarget) * Quaternion.AngleAxis($$anonymous$$athf.Clamp(-enemyVelocity.x * xBankSpeed, -xBankRange, xBankRange), Vector3(0,0,1))* Quaternion.AngleAxis($$anonymous$$athf.Clamp(enemyVelocity.z * zBankSpeed, -zBankRange, zBankRange), Vector3(1,0,0))
Thanks maccabbe, your comment helped quite a bit! It still banks in world space but combining the two rotations into one made all the difference. Then, if the LookRotation is placed at the end the desired result is achieved and works in local space.
Quaternion.AngleAxis($$anonymous$$athf.Clamp(-enemyVelocity.x * xBankSpeed, -xBankRange, xBankRange), Vector3(0,0,1)) * Quaternion.AngleAxis($$anonymous$$athf.Clamp(enemyVelocity.z * zBankSpeed, -zBankRange, zBankRange), Vector3(1,0,0)) * Quaternion.LookRotation(lookAtTarget)
Do you want to post your last comment as an answer so I can approve it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613200639im_/https://answers.unity.com/themes/thub/images/avi.jpg)