Skip to content

markulie/unity-smooth-follow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Smooth Follow with Lerp for Unity

private void Update()
{
    speed = sliderSpeed.value;
    follower.transform.position = Vector3.Lerp(follower.transform.position, transform.position + followerOffset, speed);
    if (lookAt.isOn) follower.transform.LookAt(transform.position);
}

private void OnMouseDown()
{
    screenPoint = Camera.main.WorldToScreenPoint(transform.position);
    offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

private void OnMouseDrag()
{
    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    transform.position = curPosition;
}