First and foremost, I know that this question seems to have been asked previously, but it hasn’t been answered. At least, not in the manner I wanted it to be…
I have a camera high up in the sky at the beginning of the game. After pressing the End Turn Button, I want to rotate its X axis with 15 degrees (down) and then modify its Y transform position from 12 to 3. But I want all these to be smooth. I know that what I want is somehow related to Time.deltaTime or the so-called Lerp function, but I couldn’t understand anything from the previous answers. Can someone explain all these for dummies? To make it more simple, I already know the moment the player presses the End Turn Button.
The function applyClick is called at On Click property of the inspector, so the EndTurn Button clicking is not the problem. The problem is how do I make the camera do this (15 degrees rotating over its x axis and then going downwards with 10 units) in like 3 or 4 seconds. Please, have mercy and explain it as if I were the biggest dummy you’ve ever seen. Thanks a lot for your time!
The for loop at the end there is extremely handy in many, many situations like this. You can use any Lerp function (or in the case of rotations, Slerp) there in exactly the same way, and smoothly transition from one value to the other value over a given duration.
Adding in the rotation to that function is left as an exercise for the reader. Let me know if you need more help
I’m impressed! It worked! The only thing I added is that this script was not attached to the camera and I declared it as a game object and then used it as mainCam.transform.position…
I have one more question, what can I do if I also want to rotate it? (also smoothly)
Rather than using Lerp you could also try using MoveTowards() or SmoothDamp() for the movement. The advantage to using either of those over Lerp is they don’t slow down as the camera approaches the end target position. You could try putting your logic in LateUpdate as well which is recommended for camera movement.
But in the code example provided, the third parameter changes, while the first parameter is set at the start and remains constant until the end. This code will make it move linearly.
I tried this but I do not fully understand what you’re saying due to the fact that I don’t even know what Quaternion is. I just use it when spawning an object
EDIT: I also put from empty game object in the scene and set its rotation X at -15 and put to game object in the scene and set its rotation X at 15. I dragged and dropped them onto the script, but now if I run, after clicking the button, it only rotates it 0,0121 deegres, or one random value between 0,0001 and 0,01…
EDIT2: I looked up for Quaternion in the documentation and it seems to tell me the rotation of an gameobject using the Quaternion.identity (it’s read-only). So if I go ahead and set Quaternion.identity as a new parameter and then write a transform.Rotate = something, will it work?
A Quaternion is just like a Vector3, but for rotations instead of positions. Quaternion.identity is one (neutral/non-rotated) value of that type, just like Vector3.zero is (0, 0, 0).
The function with rotations added to it might look like:
In case anyone comes on here and wants a nice clean answer.
Put this block of code into your script if you want to lerp one object to another location over a given amount of time. It’s just a nice handy function that I’ve reused for years.
public IEnumerator MoveCameraToPosition(Transform _cameraToMove, Transform _locationToMoveTo, float _timeToMove)
{
Vector3 currentPos = _cameraToMove.transform.position;
float t = 0f;
while (t < 1)
{
t += Time.deltaTime / _timeToMove;
_cameraToMove.transform.position = Vector3.Lerp(currentPos, _locationToMoveTo.position, t);
if (t > 0.8f)
currentlyFollowing = _locationToMoveTo;
yield return null;
}
}
This way you can reuse it for literally any object
Rotation works just the same, add a line referencing the current Rotation in a vector 3, then get the rotation of the location you’re going to, then lerp in the same manner