- Home /
Unable to move Gameobject properly and smoothly to where mouse clicked , using Vector3.Lerp
Hello folks, Right now i'm making a script which moves a gameobject (named Player), to a specific location with use of Vector3.Lerp function.
The problem is that i want the Player to move smoothly, but when i click the mouse button, Player moves suddenly from where it is right now to where i clicked.
I attached this script to another gameobject(not the Player) due to necessity and cannot attach this script to Player gameobject. here is the code:
using UnityEngine;
using System.Collections;
public class ClickcharacterController : MonoBehaviour {
private GameObject Player;
public float speed = .1f;
void Start () {
if(Player == null){
Debug.Log("null");
Player = GameObject.FindGameObjectWithTag("Player");
}
}
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse1))
{
Vector3 mousePos=new Vector3( Camera.main.ScreenToWorldPoint (Input.mousePosition).x,Camera.main.ScreenToWorldPoint (Input.mousePosition).y,0);
if(Vector3.Distance(mousePos, Player.transform.position) > 0.2f && Vector3.Distance(mousePos, transform.position) <3f ){
Vector3 dir = new Vector3(mousePos.x - Player.transform.position.x,mousePos.y - Player.transform.position.y,0);
Player.transform.position =Vector3.Lerp( Player.transform.position, mousePos,speed);
}
}
}
}
Thanks
Depending on how fast your scene is running, this can happen. Have you tried multiplying your speed parameter by the time delta?
Player.transform.position =Vector3.Lerp( Player.transform.position, mousePos,speed * Time.deltaTime);
Are you sure lerping is the behavior you want in this case? Have you tried Vector3.$$anonymous$$oveTowards?
Thanks Josh my problem was solved. Thanks anyway
Answer by amirm57 · May 17, 2016 at 05:44 PM
I found it! The problem was that i put the whole code into the if (Input.GetKeyDown (KeyCode.Mouse1)) { }
thank you anyway
Your answer
![](https://koobas.hobune.stream/wayback/20220612081008im_/https://answers.unity.com/themes/thub/images/avi.jpg)