- Home /
Camera freezes while moving, game is not locked up.
I'm debugging an activity where our camera will move to a location / rotation, the user will perform a small task, and then the camera moves to the next location / rotation, and this cycle repeats until the activity is complete. Also, this activity is loaded as a prefab, meaning when the user engages the activity, we place a prefab somewhere in the world, and move the camera to that location and let the prefab "take control" by disabling the player while it is active.
The issue is that the main camera will freeze right when we activate this activity prefab. The GUI camera and game logic are still working, as we can still interact with gui elements, and we can see in scripts that the game logic is still going. In order to get the camera to unfreeze, I have to make Unity lose focus (alt-tab or click on another window) and then come back to it. The camera will then start rendering the activity like normal. However, the issue occurs again during the activity, while the camera is moving to a new location.
Now, I have successfully stopped this from happening by pulling in the far plane for the main camera to a very small number (10), but this prevents the scenery around the activity from being rendered and well... it just looks bad. That leads me to suspect that it has something to do with culling, but really I'm at a loss here.
Has anyone seen this occur? Does anyone know a way to prevent it from happening?
Thanks for your time.
EDIT: Here is the script controlling the camera for the prefab. I did not write it.
using UnityEngine;
using System.Collections;
public class RoofCam : MonoBehaviour {
private RoofNode cameraNode;
public float slerpTime = 1f;
public float backDistance = 4.5f;
public RoofNode CameraNode {
get { return cameraNode;}
set {
setNode(value);
}
}
// Use this for initialization
void Start () {
basePosition = Camera.mainCamera.transform.position;
baseOrientation = Camera.mainCamera.transform.rotation;
toPosition = basePosition;
toOrientation = baseOrientation;
}
// Update is called once per frame
//private float currentTime = 0;
void Update () {
//Sway();
//setNode(cameraNode);
if(basePosition != toPosition){
currentTime += Time.deltaTime;
if(currentTime <= slerpTime){
float slerpStep = currentTime/slerpTime;
Vector3 newPos = Vector3.Slerp(basePosition, toPosition, slerpStep );
Quaternion newOri = Quaternion.Lerp(baseOrientation, toOrientation, slerpStep);
Camera.mainCamera.transform.position = newPos;
Camera.mainCamera.transform.rotation = newOri;
}
else{
basePosition = toPosition;
baseOrientation = toOrientation;
currentTime = 0;
}
}
}
Vector3 swayToPos;
Vector3 swayToExtents = new Vector3(1, 1, 1);
Quaternion swayToOri;
float swayTime = 1.0f;
float currentTime = 0f;
float restTime = .5f;
bool toBasePosition = false;
bool isResting = true;
void Sway()
{
//periodically set swayToPos
//bound to extents
currentTime += Time.deltaTime;
if(currentTime >= swayTime)
{
//flip
if(!toBasePosition){
swayToPos = new Vector3(Random.Range(-swayToExtents.x + basePosition.x, swayToExtents.x+basePosition.x),
Random.Range(-swayToExtents.y + basePosition.y, swayToExtents.y + basePosition.y),
Random.Range(-swayToExtents.z + basePosition.z, swayToExtents.z + basePosition.z));
}
else{
swayToPos = basePosition;
}
currentTime = 0f;
isResting = true;
}
if(isResting){
if(currentTime > restTime){
currentTime = 0;
isResting = false;
}
}
Vector3 newPos = Vector3.Lerp(transform.position, swayToPos, currentTime/swayTime);
transform.position = newPos;
}
Vector3 basePosition ;
Quaternion baseOrientation ;
Vector3 toPosition ;
Quaternion toOrientation ;
void setNode(RoofNode node)
{
if(node == null)
return;
if(cameraNode == null)
{
basePosition = node.Position;
baseOrientation = node.Rotation;
Camera.mainCamera.transform.rotation = baseOrientation;
Camera.mainCamera.transform.position = basePosition;
}
else if( cameraNode != node)
{
currentTime = 0;
baseOrientation = Camera.mainCamera.transform.rotation;
basePosition = Camera.mainCamera.transform.position;
}
cameraNode = node;
this.toOrientation = node.Rotation;
this.toPosition = node.Position + ( node.Forward * -backDistance);
}
}
You should post the prefab script - apparently it's the responsible for freezing the camera.
Good suggestion aldonaletto, updated question with the script that controls the camera for the prefab.
Your answer
Follow this Question
Related Questions
How to Freeze move camera 3 Answers
Freeze an Axis Rotation Help? 0 Answers
Camera Change Bug 0 Answers