Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development Cameras in Unity Camera Movement Adding Camera Bob

Kyle Rosse
Kyle Rosse
11,857 Points

Camera is zooming way far out

Hey everyone, the camera is zooming super far out on the x and z axis when I run this script

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

public class OrbitCamera : MonoBehaviour {

[SerializeField]
private Transform target;
[SerializeField]
private float rotationSpeed = 10f;

[SerializeField]
private float bobPeriod = 2f;
[SerializeField]
private float bobAmplitude = 2f;

private float startY;
private float bobDistance;

// Use this for initialization
void Start () {

    //store the start position of the camera
    startY = transform.localPosition.y;

}

// Update is called once per frame
void Update () {

    //update the vertical camera bob
    bobDistance = Mathf.Sin(Time.timeSinceLevelLoad / bobPeriod) * bobAmplitude;

    transform.position = new Vector3 (transform.localPosition.x, startY + bobDistance, transform.localPosition.z);

    //update camera position
    transform.RotateAround (target.position, Vector3.up, rotationSpeed * Time.deltaTime);

    //update the camera look
    transform.LookAt (target);

}

}

1 Answer

Wesley Trayer
Wesley Trayer
13,812 Points

I don't see a mistake. I also ran your code in an already working version of the game and it seemed to run just fine.

Is your "[SerializeField]-target" set to "board"? If it were set to "Directional Light", for instance, it would fly right out of the room. :)

Wesley Trayer
Wesley Trayer
13,812 Points

Just encountered this problem, I removed the script component and then reapplied it. No more problem!

Puzzling.