- Home /
What is the best way to Lerp on only one axis?
I have a turret. this turret is using a very simple script to slowly rotate to where the crosshair is pointing
Vector3 direction = target.transform.position - transform.position;
direction.y = 0.0f; //only rotate on y axis
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotationVelocity * Time.deltaTime);
This works great, however, I have a gun attached to the rotating turret that I want to smoothly rotate up and down to where the crosshair is. When I use a quaternion.lerp on only one axis, the gun still smoothly turns toward the final destination. Is there a way I can make it so that the gun is fixed to the turret but can smoothly rotate up and down?
Answer by unity_ek98vnTRplGj8Q · Jul 23, 2020 at 08:00 PM
Make the gun a child of the turret, then you can lerp just to the vertical value of the cursor. I haven't tested this code, but hopefully it works
//Your turret rotation code goes here
Vector3 direction = target.transform.position - transform.position;
//Pythagorean theorem to get the target positioned in front of gun
Vector3 gunDirection = new Vector3(0, direction.y, Mathf.Sqrt((direction.x * direction.x) +(direction.z * direction.z));
//Lerp using localRotation
Quaternion targetRotation = Quaternion.LookRotation(gunDirection, transform.up);
gun.transform.localRotation = Quaternion.Lerp(localRotation, targetRotation, gunRotateSpeed * Time.deltaTime);
This is brilliant! Just 2 things, there should be 3 parentheses' after the vector3, and in the Quaternion.Lerp it should be gun.transform.LocalRotation. Thank you very much!
I'm only 5 months into the Unity world, but I'm surprised at how often I see answers with "I haven't tested this code..."
Why do people post code they haven't tested? In this case, the answer mostly works (the localRotation variable doesn't exist in this context, use transform.localRotation instead, and as mentioned there is a closing parenthesis missing), but I'd argue that posting tested code is more valuable than taking best guesses that might not work, while misleading others along the way.
When people post questions like this they are usually asking about general ideas. The important bit of this is not the exact syntax, but the direction in which the solution is approached. The asker did not need to be given the exact code to help solve his problem, just a push in the right direction.
The purpose of this forum is not to give people copy and paste code solutions for all of their problems, it is to help them understand Unity better so that they can create their own solutions to problems. If you try code and it doesn't work for you or it gives you syntax errors and you can't figure it out on your own then you can always ask for more help.
In my case I usually have very limited time to answer questions. Rather than take 20 $$anonymous$$utes to create a new project (assu$$anonymous$$g I'm even on a computer with Unity, which often I am not) , set up game objects in a scene, import this code, then test it I would rather just take 2 $$anonymous$$utes to type up some pseudocode in a text editor that is just as useful. If I had to take 20 $$anonymous$$utes to test every question I answered, then I would not answer very many questions.
Of course in an ideal world everyone would post tested code that is guaranteed to work. But best guesses are still more helpful than no answer at all. If code doesn't work then it won't be marked as the correct answer. If it is almost correct, then there can be a discussion on what needs to be fixed. I post untested code because I know that if I don't there's a decent chance that the question will go unanswered all together.
Your answer