Question by
centelleo · Nov 18, 2015 at 02:49 PM ·
transformonmousedown
OnMouseDown transform problem
Hi I'm trying to make a code so that if an object is clicked, it will slide towards another object. Slide the whole way there, not one inch closer every time I click the object.
This is my script using UnityEngine; using System.Collections;
public class OnMouseDownWolf : MonoBehaviour {
public Transform target;
public float speed;
void Update(){
if (Input.GetMouseButtonDown(0))
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
}
So far the object will move 1 inch towards to target everytime I click said object. But I'd like it so that when I click the object, it will slide the whole way there.
Thank you.
Comment
Try looking into coroutines and/or lerps. You need the Vector3.$$anonymous$$oveTowards() function to get called multiple times over multiple frames to get the effect you want. Calling it once will only make the object move the amount specified by step.
Your answer