We will need:
First, you need to set the position of your camera. Let's say I want to show the whole game field like this:
Add a new Position Dummy (that little arrow - like object) to the Scene view. It will most likely have some weird position values. But we're going to fix that.
Select your camera and copy it's transform component values (Transform right click -> Copy Component):
Select your newly added Position Dummy object and choose Paste Component Values:
That's it! Now you saved your camera position. Add a few more, three of four will do. This is my scene overview (it has four points total so far, one is out of the screen):
Great!
It's time to start scripting!
We should be able to smoothly move our camera between these points. For simplicity, we will move it cyclically from one point to another.
So our script should have an array of Transform objects, speed value and some moving functionality. So here's the first part of our script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraTweener : MonoBehaviour {
public Transform tweenableCamera;
public List<Transform> positions;
public float smoothing = 7f;
}
We'll need a variable to store our camera. List of Transform components represents all of our Camera Positions.Create a new empty game object with GameObject->Create Empty and name it Camera Tweener. It's position doesn't matter at all, so don't bother changing it. At this time it should have three empty variable fields:
If you have 4 positions, type 4 in Positions Size field. It will open new slots for your transform objects. Drag and drop each of your Position Dummies there:
You should also drop your camera there. You can skip this part with "custom" camera, because you can always get currently active camera via script, but for this tutorial we will keep it as it is.
Now when you have filled your List of Transform objects, time to code further.
We will check if a user clicked on the game field, and if it happens so, we will move camera to the next point. If we reach the last point and click again - it will move back to the first one. Here we go:
1: using UnityEngine;
2: using System.Collections;
3: using System.Collections.Generic;
4: public class CameraTweener : MonoBehaviour {
5: public Transform tweenableCamera;
6: public List<Transform> positions;
7: public float smoothing = 7f;
8: private int _currentPosition;
9: private CameraStoppedTweeningDelegate _tweenStoppedCallback;
10: // Use this for initialization
11: void Start () {
12: if (positions.Count != 0)
13: {
14: _currentPosition = 0;
15: tweenableCamera.transform.position = positions[_currentPosition].position;
16: tweenableCamera.transform.rotation = positions[_currentPosition].rotation;
17: }
18: }
19: }
We've added one variable: _currentPosition. We will store our current Position index, which we can use to calculate our next point.When the script starts, it checks if there's anything in our positions list. And then it takes our camera to the first position. It also rotates it correspondingly.
Check and see it now - your camera should jump to the first given position when you go to Play Mode.
Great, now let's create some movement!
1: using UnityEngine;
2: using System.Collections;
3: using System.Collections.Generic;
4:
5: public class CameraTweener : MonoBehaviour {
6:
7: public Transform tweenableCamera;
8: public List<Transform> positions;
9: public float smoothing = 7f;
10:
11: private int _currentPosition;
12: private CameraStoppedTweeningDelegate _tweenStoppedCallback;
13: // Use this for initialization
14: void Start () {
15: if (positions.Count != 0)
16: {
17: _currentPosition = 0;
18: tweenableCamera.transform.position = positions[_currentPosition].position;
19: tweenableCamera.transform.rotation = positions[_currentPosition].rotation;
20: }
21: }
22:
23: // Update is called once per frame
24: void Update () {
25: if (Input.GetMouseButtonDown(0))
26: {
27: if (++_currentPosition >= positions.Count)
28: {
29: _currentPosition = 0;
30: }
31: SetPosition(_currentPosition);
32: }
33: }
34:
35: public void SetPosition(int positionID)
36: {
37: StopCoroutine("MoveToPosition");
38: StartCoroutine("MoveToPosition", positionID);
39: }
40:
41: private IEnumerator MoveToPosition(int positionID)
42: {
43: Vector3 targetPosition = positions[positionID].position;
44: Quaternion targetRotation = positions[positionID].rotation;
45:
46: while (Vector3.Distance(tweenableCamera.transform.position, targetPosition) > 0.05f)
47: {
48: tweenableCamera.transform.position = Vector3.Lerp(tweenableCamera.transform.position, targetPosition, smoothing * Time.deltaTime);
49: tweenableCamera.transform.rotation = Quaternion.Lerp(tweenableCamera.transform.rotation, targetRotation, smoothing * Time.deltaTime);
50: yield return null;
51: }
52: yield return null;
53: }
54: }
Oh, lots of code.. But it's all simple.In our Update() method we check, if a user clicked somewhere, if so, we check if the next _currentPosition will go out of bounds of our positions List. If so, we set our first position as the target.
Then we call our new SetPosition(_currentPosition) method. It stops any coroutine named "MoveToPosition" if it's running and starts a new one.
Coroutines are great way to animate something without storing any time variables.
We need to know our next Point location (position) and rotation. We create two variables named targetPosition and targetRotation to store that.
Now is the tricky part. The body of the while loop will be called once every frame because we added yield return null at the end of it. It's the essence of the Coroutines. You can get more information here.
The body of this while loop will calculate how much we need to move and rotate our camera towards the target point, and will change our camera position and rotation respectively. That magic functions Vector3.Lerp() and Quartenion.Lerp() do the trick. Because we change our camera's position and rotation a bit in every frame, it creates animation. Fascination, isn't it?






No comments:
Post a Comment