Nikita Chernykh
Nikita Chernykh

Reputation: 270

Camera Moves to wrong direction when i make it Active. Unity5

Basically i have 2 cameras in the scene and i make Second camera active by pressing a button. When it gets active i want it to move from its original position to position i specifically put in my script. But what happening is that when second camera gets active it moves somewhere far away.

I placed my second camera on the place i want it to be and recorded the x/y/z position and rotation then i put it in script as startPosition. After that i moved camera a bit away and put in its Start() to move to its startPosoton. So when i activate this second camera it should move from position it is at to this startPosition but it's not.

did someone have this problem?

here is parts of my code: so when second camera gets active it does this

public float smoothTime = 2;
private Vector3 newPosition;
private Quaternion newRotation;

private Vector3 startPosition = new Vector3 (-139.56f,6.58f,21.82f);
private Quaternion startRotation = Quaternion.Euler(0.22f,212.71f, 0f);

void Awake(){
    newPosition = transform.position;
    newRotation = transform.rotation;
}

void Start () {
    newPosition = startPosition;
    newRotation = startRotation;
}

void Update () {
    PositionAndRotationChanging ();
}

void PositionAndRotationChanging () {
    transform.position = Vector3.Lerp 
    (transform.position,newPosition,Time.deltaTime * smoothTime);

    transform.rotation = Quaternion.Slerp(transform.rotation, newRotation,    
    Time.deltaTime * smoothTime);

}

Upvotes: 0

Views: 88

Answers (1)

rutter
rutter

Reputation: 11452

I think you've misunderstood what lerp does. "Linear interpolation" takes two values and returns another value that is t percent between them. If you never pass in a t of zero or one, you'll never see either endpoint represented in the output.

Typically, you might pass in a t that increases over time. For example, you could have a float timer that increases by Time.deltaTime once per frame:

float timer;
Vector3 startPosition = Vector3.zero;
Vector3 endPosition = Vector3.up * 10f;

//move from "startPosition" to "endPosition" over 20 seconds
void Update() {
    timer += Time.deltaTime;
    transform.position = Vector3.Lerp(startPosition, endPosition, timer / 20f);
}

In your case, you're keeping t more or less constant. It'll be roughly equal to smoothTime times 0.02 or so. By repeatedly moving your transform.position closer to endPosition, you'll produce an interesting easing effect -- because your output will always be, say, 40% closer.

The way your code is written, you might prefer to use MoveTowards and RotateTowards, instead:

public class ExampleClass : MonoBehaviour {
    public Transform target;
    public float moveSpeed;
    public float rotSpeed;

    void Update() {
        float move = moveSpeed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, move);

        float rot = rotSpeed * Time.deltaTime;
        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, rot);
    }
}

Those helper functions work differently, and allow you to move X units closer to some point, or rotate Y degrees closer to some orientation.

Upvotes: 1

Related Questions