0

I am using Unity3d and I have Vector3.Lerp problem. In my initial code Lerp code working normal, but the second Lerp in PrevView method is not working correctly. Camera just shakes and returns to it's previous position. Where am I making mistake?

Code here:

using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour
{
   Vector3 camPos;
   Vector3 startPos; // for storing Camera's first position
   Transform camTr;
   float speed = 5f;**strong text**

void Start()
{
    camTr = Camera.main.transform;
    camPos = camTr.position;
    startPos = camTr.position;
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Buildings")
        {
            var buildings = GameObject.FindGameObjectsWithTag("Buildings");
            foreach (GameObject go in buildings)
            {
                if (go == hit.collider.gameObject)
                {
                    camPos.x = go.transform.position.x;
                    //camPos.y = go.transform.position.y + 30;
                    camPos.z = go.transform.position.z - 20;
                }
                else
                {
                    go.SetActive(false);
                }
            }
        }
    }
    camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
}

public void PrevView()
{
    camTr.position = Vector2.MoveTowards(camTr.position, startPos, Time.deltaTime * speed);
}

}

Anton K
  • 4,658
  • 2
  • 47
  • 60

3 Answers3

1

-First of all its not a "second Lerp" its a MoveTowards function.

-Lerp isnt supposed to take a Time.deltaTime * speed its supposed to be a normalized float of where along the path to be.

-In other words, just use MoveTowards if your gonna pass the variables your passing now.

-Your MoveTowards function should be Vector3 , im sure thats what you meant to put, right?

-PrevView() should be an IEnumerator, written as such:

float threshold = 0.01f;
IEnumerator PrevView()
{
    while((camTr.position - startPos).sqrMagnitude > threshold)
    {
        camTr.position = Vector2.MoveTowards(camTr.position, startPos, Time.deltaTime * speed);
        yield return null;
    }
}

then use StartCoroutine("PrevView"); to call the IEnumerator

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
  • Thanks, Andrew. I did everything you wrote. But when I press Button that calls StartCoroutine("PrevView"), Unity freezes and not responding –  Aug 19 '15 at 04:58
0

Try changing your PrevView function like this:

IEnumerator PrevView()
{
    float threshold = 0;
    float increment = Time.deltaTime * speed;
    while(threshold < 1)
    {
        camTr.position = Vector3.Lerp(camTr.position, startPos, threshold);
        threshold  += increment;
        yield return new WaitForSeconds(0.02f);
    }
}

And call it with StartCoroutine. I haven't tested so let me know if you are having trouble.

Reasurria
  • 1,808
  • 16
  • 14
  • Thanks Reasurria, but it's the same thing. Camera moving little bit, then returns to previous position –  Aug 19 '15 at 07:37
  • I've updated. It should be fine now as long as startPos is where you want the camera to be at the end of everything. – Reasurria Aug 19 '15 at 09:54
0

I solved the problem. Many thanks for trying help me

My code here:

using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour
{
    Ray ray;
    RaycastHit hit;
    Vector3 camPos;
    Vector3 startPos; // for storing Camera's first position
    Transform camTr;
    float speed = 2.5f;
    bool b;  // avoiding

void Start()
{
    b = true;
    camTr = Camera.main.transform;
    camPos = camTr.position;
    startPos = camTr.position;
}

void Update()
{
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
    {
        if (hit.collider.tag == "Buildings")
        {
            var buildings = GameObject.FindGameObjectsWithTag("Buildings");
            foreach (GameObject go in buildings)
            {
                if (go == hit.collider.gameObject)
                {
                    camPos.x = go.transform.position.x;
                    camPos.y = go.transform.position.y + 30;
                    camPos.z = go.transform.position.z - 20;
                }
                else
                {
                    go.SetActive(false);
                }
            }
        }
    }
    if (b)
    {
        camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
    }
}

public void PrevView()
{
    b = false;
    StartCoroutine("MoveBack");
}

IEnumerator MoveBack()
{
    while ((camTr.position - startPos).sqrMagnitude > 0.01)
    {
        camTr.position = Vector3.MoveTowards(camTr.position, startPos, Time.deltaTime * speed * 10);
        yield return new WaitForSeconds(0.001f);
    }
}

}