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