This is the code I use for my Camera movement:
using UnityEngine;
using System.Collections;
public enum CameraState
{
CameraGoUp,
CameraGoDown,
CameraStandard
}
[RequireComponent(typeof(Camera))]
public class CameraBehaviour : MonoBehaviour {
public GameObject player;
public CameraState cameraState = CameraState.CameraStandard;
private float cameraX;
private float cameraZ;
private float cameraY;
private float currentY;
private float i;
public float riseAmount = 10;
public float fallAmount = 10;
public float riseSpeed = 0.1F;
public float fallSpeed = 0.1F;
void Update()
{
currentY = transform.position.y;
CameraMovement();
}
void LateUpdate()
{
DetermineCoordinates(player.transform.position.x, player.transform.position.z);
Vector3 cameraTransform = new Vector3 (cameraX, cameraY,cameraZ);
transform.position = cameraTransform;
IReset();
}
private void DetermineCoordinates(float playerX, float playerZ)
{
cameraX = playerX + 30;
cameraZ = playerZ - 10;
cameraY = currentY;
}
private void CameraMovement()
{
switch (cameraState)
{
case CameraState.CameraGoUp:
if (i < riseAmount)
{
currentY = currentY + riseSpeed;
Debug.Log("Camera is moving up");
i = i + riseSpeed;
}
break;
case CameraState.CameraGoDown:
if (i < fallAmount)
{
currentY = currentY - fallSpeed;
Debug.Log("Camera is moving down");
i = i + fallSpeed;
}
break;
case CameraState.CameraStandard:
break;
}
}
private void IReset()
{
if (i != riseAmount)
{
Debug.Log("i is not riseAmount");
}
if (i == riseAmount)
{
Debug.Log("i is riseAmount");
i = 0;
Debug.Log("i has been reset!");
cameraState = CameraState.CameraStandard;
} else if (i == fallAmount)
{
Debug.Log("i is fallAmount");
i = 0;
Debug.Log("i has been reset!");
cameraState = CameraState.CameraStandard;
}
}
}
Now, this is what triggers the movement:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("CameraMoveUp"))
{
cameraMoveUp = other.gameObject.GetComponent<CameraMoveUp>();
cameraBehaviour.riseAmount = cameraMoveUp.riseAmount;
cameraBehaviour.cameraState = CameraState.CameraGoUp;
} else if (other.gameObject.CompareTag("CameraMoveDown"))
{
cameraMoveDown = other.gameObject.GetComponent<CameraMoveDown>();
cameraBehaviour.fallAmount = cameraMoveDown.fallAmount;
cameraBehaviour.cameraState = CameraState.CameraGoDown;
}
}
The problem with this system is that, for some reason, i is never reset to 0 because it is apparently never equal to rise- or fallAmount. I’ve checked in the inspector’s debug mode though, and it says that:
I = 10
Rise Amount = 10
Fall Amount = 10
So why is it not resetting i?
i obviously has the exact same value as the other two.
Help is greatly appreciated, since this means that I can only move the camera once per scene.