Camera Field of View
SOLVED
float fov;
public Transform obj1, obj2;
float distance;
// Use this for initialization
void Start () {
Camera.main.fieldOfView = fov;
fov = 60;
}
// Update is called once per frame
void Update () {
distance = Vector3.Distance (obj1.position, obj2.position);
Debug.Log (distance);
fov = 60;
if (distance < 4) {
fov = Mathf.Lerp (fov, 40, Time.deltaTime);
} else {
fov = Mathf.Lerp (fov, 60, Time.deltaTime);
}
if (distance > 10) {
fov = Mathf.Lerp (fov, 70, Time.deltaTime);
} else {
fov = Mathf.Lerp (fov, 60, Time.deltaTime);
}
}
}
You neverreassign fov
to Camera.main.fieldOfView
so the camera's field of view will never change....
Ok, back to start... Why did you delete all the content of your question? This would render this question and it's answers completely useless. Also you closed the question with the reason "Question answered, right answer accepted" however you haven't accepted any answer. You haven't even upvoted anything.
UnityAnswers is a knowledge base. Removing the original question would mean you don't want anyone else to read it / to learn anything from it. That's not how Unity Answers work. I've reverted your last edit and also reopened the question.
If you found a solution to your problem, feel free to post an answer. It's fine to answer your own question if nobody else did. If your problem is outdated / no longer relevant you can close it with an appropriate reason. However since you replaced your question with the words "Already Solved" it would be nice to actually answer your question.
I didn't put any answer as right cause @Hellium was right. I just messed up with the order in the void Start (). (Already updated my question with the right code).
Answer by Bunny83 · Nov 08, 2017 at 11:41 PM
Your problem is that you never actually set the fieldOfView of the Camera. In addition you have two seperate distance checks and each one has an else case. So you will always execute two lerps at the same time which will most likely doesn't give you the desired effect. Further more you set your "fov" variable to 60 each update which renders the lerping and your conditions useless. You probably wanted to do something like:
void Update()
{
distance = Vector3.Distance (obj1.position, obj2.position);
if (distance > 10)
{
fov = Mathf.Lerp (fov, 70, Time.deltaTime);
}
else if (distance > 4)
{
fov = Mathf.Lerp (fov, 60, Time.deltaTime);
}
else
{
fov = Mathf.Lerp (fov, 40, Time.deltaTime);
}
Camera.main.fieldOfView = fov;
}
I don't understand this use of $$anonymous$$athf.Lerp. It seems to me that it should make the b input an asymptote rather than a destination because it is constantly moving the a input and always keeping the t input small.
Oh, I see. You're just using the lines OP wrote...
Answer by JasonBennett · Oct 29, 2017 at 03:43 PM
My guess is that you need to remove the line of code at the start of Update() that sets your fov variable to 60. Leave it in Start(). Otherwise your program will conditionally do the Lerp every frame, and then reset the fov value at the start of the next frame, so effectively you will never see the new fov.
void Update () {
distance = Vector3.Distance (obj1.position, obj2.position);
Debug.Log (distance);
// Remove this --> fov = 60;
@curious$$anonymous$$oala Still never changes. Even if I put manually field of view for example 30 before clicking play, it's still 30 when I click play.