Problem with Vector3.lerp and Time.deltatime

I simply want to move an object here is my code:
But it doesn’t work, every time i use Time.deltaTime in the script the position coordinates of the game object are not like the values in the variable. The position change to something else which is not insight of the camera.

Here is the hole script which is attached to the object:

using UnityEngine;
using System.Collections;

public class detailzoom_alaska : MonoBehaviour {
	
    Vector3 karten_position = new Vector3(0.9578142f,0.2050258f, -0.0404333f);
    Vector3 ausserhalb_karte_position = new Vector3(0.03719311f,0.3353992f, 0.008037157f);
	public float smooth=1;
	private static GameObject clickedGmObj2;	
	
		GameObject GetClickedGameObject()
		{
		    // Builds a ray from camera point of view to the mouse position
		    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		    RaycastHit hit;
		    // Casts the ray and get the first game object hit
		    if (Physics.Raycast(ray, out hit, Mathf.Infinity))
		       return hit.transform.gameObject;
		    else
		       return null;
		}
		
	void PositionChanging ()
    {
		if(Input.GetMouseButtonDown(0))
		{
			clickedGmObj2 = GetClickedGameObject();
			if(clickedGmObj2)
			{
				if (clickedGmObj2.name=="alaska")
					{
					 Vector3 neue_position = ausserhalb_karte_position;
					 transform.position = Vector3.Lerp(transform.position, neue_position, smooth * Time.deltaTime);
					}
			}
		}
              
		if(Input.GetMouseButtonDown(1))
		{
			clickedGmObj2 = GetClickedGameObject();
			if(clickedGmObj2)
			{
				if (clickedGmObj2.name=="alaska")
					{
					 Vector3 neue_position = karten_position;
					 transform.position = Vector3.Lerp(transform.position, neue_position, smooth * Time.deltaTime);
					}
			}
		}
    }
	
	// Update is called once per frame
	void Update () 
	{
	PositionChanging();
	}
}

Does anyone have an idea why this code isn’t working ?

If i use instead a code with position change without lerp it works without errors, but i think lerp is a cool thing and i want to use it :slight_smile:
Flo

Debug.Log() out the values that go into and come out from Lerp method. Debug class with it’s Log and DrawLine static methods give you lots of power to iron out whatever errors you may have here and in the future. Also, in this case you have quite a lot of duplicate code, shortening any code before trying to debug it makes your life easier.

A lerp works by changing from/to a value over time or over multiple frames. A single mousebuttondown event occurs only during one frame. Change the ‘neue_position’ to be initialized with your other vector3 declarations, and then move the Lerp line outside of the if-statements.

void PositionChanging ()
    {
		if(Input.GetMouseButtonDown(0))
        {
			clickedGmObj2 = GetClickedGameObject();
            if(clickedGmObj2  clickedGmObj2.name=="alaska")
			{
	           neue_position = ausserhalb_karte_position;
            }
        }

       	if(Input.GetMouseButtonDown(1))
        {
            clickedGmObj2 = GetClickedGameObject();
            if(clickedGmObj2  clickedGmObj2.name=="alaska")
            {
	        	neue_position = karten_position;
            }
        }
		transform.position = Vector3.Lerp(transform.position, neue_position, smooth * Time.deltaTime);
    }

Thanks for help ! That’s it the Mousebutton is only one frame, thats the Bug :wink: