1
\$\begingroup\$

I want that at some point in the game the player will be looking over the view out of the window. So I added a new Camera and positioned the camera close to the window. Then using a small script I'm turning the Camera on.

But after turning the camera on I wan to make that the camera will rotate around the window area so it will give a natural feeling that the player is looking over the view outside.

Window Camera

And the script that is attached to another GameObject that activating the Camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemAction : MonoBehaviour
{
    public Camera viewCamera;

    private void Update()
    {

    }

    public void ViewCamera()
    {
        viewCamera.enabled = true;
    }
}

The part I'm using is the ViewCamera method. But I messed it up. But the idea is after turning on the Camera to make it feel like looking around the view outside.

How can I make the camera move first to one direction and then mirrored to the other direction ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemAction : MonoBehaviour
{
    public Camera viewCamera;
    public float Speed;

    public void ViewCamera()
    {
        viewCamera.enabled = true;
        StartCoroutine(animate());
    }

    IEnumerator animate()
    {
        while (transform.localEulerAngles.y > -45)
        {
            transform.Rotate(0, -Time.deltaTime * Speed, 0);
            yield return null;
        }
        //and then possibly the same but mirrored to look in the other direction
        while (transform.localEulerAngles.y < 45)
        {
            transform.Rotate(0, Time.deltaTime * Speed, 0);
            yield return null;
        }
    }
}

The way it is now it's just keep rotating to the same direction nonstop.

\$\endgroup\$
1
\$\begingroup\$

The easiest is probably to attach an Animator component to your camera, create an animation for it and then you can add keyframes to change the camera's yaw (and possibly position as well which could make it look more natural). From your script, grab the Animator component and trigger the animation, then return to the main camera once it's over (presumably).

Another approach could be setting up a simple coroutine to do that yourself; you'd have more control over the animation, but less freedom, so it might end up looking quite rigid. I'm talking about something like this:

IEnumerator animate(){
        while(transform.localEulerAngles.y > -45){
                transform.Rotate(0,-Time.deltaTime*Speed,0);
                yield return null;
        }
        //and then possibly the same but mirrored to look in the other direction
}
\$\endgroup\$
  • \$\begingroup\$ I'm trying first the IEnumerator animate but I can't make the mirror part. I updated edited my question with what I tried. \$\endgroup\$ – yochi le May 30 at 20:48
0
\$\begingroup\$

localEulerAngles doesn't return negative numbers. so you have to do some calculation on it before checking if its greater than -45 because if you don't, it will always be greater than -45 or any other negative number.

IEnumerator animate(){
    float angle = getRealAngle();
    while (angle > -45)
    {
        thisTransform.Rotate(0, -Time.deltaTime * speed, 0);
        angle = getRealAngle();
        yield return new WaitForEndOfFrame();
    }
    while (angle < 45)
    {
        thisTransform.Rotate(0, Time.deltaTime * speed, 0);
        angle = getRealAngle();
        yield return new WaitForEndOfFrame();
    }
}
float getRealAngle(){
    float angle = thisTransform.localEulerAngles.y;
    angle = (angle > 180) ? angle - 360 : angle;
    return angle;
}

It's always a good idea to Debug.Log values like this to keep track of what is actually happening.

void Update(){
    Debug.Log(thisTransform.localEulerAngles.y + " / " + getRealAngle());
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.