- Home /
Lerp is not working when I let go of mouse
I am trying to have a game object go to where the mouse was last positioned while clicking, but for some reason the Lerp isn't working. Rather than moving smoothly from its original position to where the mouse was last held down before letting up, it snaps directly to the mouse position, ignoring the lerp. I know this is one of many lerp related questions, so I hate to ask, but I can't find an answer that helps with what I am trying to do. Here is my code, it is the first draft so to speak, so please ignore the sloppiness:
public class Putt : MonoBehaviour {
float power;
float percentAllowed = 0.1f;
GameObject arrow;
GameObject arrowShaded;
Vector3 arrowPositionBeginning;
Vector3 mousePosition;
Vector3 arrowPositionEnd;
public bool hasHit = false;
void Start(){
arrow = GameObject.Find ("Arrow");
arrowShaded = GameObject.Find ("ArrowShaded");
arrowPositionBeginning = new Vector3 (2.2f, -2.87f, 0.0f);
arrowPositionEnd = new Vector3 (15.6f, -2.87f, 0);
}
void Update(){
if(Input.GetMouseButton (0)){
if (((arrow.transform.position - arrowPositionEnd).sqrMagnitude >= (arrow.transform.position * percentAllowed).sqrMagnitude) && !hasHit) {
Debug.Log ("Going up");
arrow.transform.localPosition += new Vector3 (0.3f, 0.0f, 0.0f);
if ((arrow.transform.position - arrowPositionEnd).sqrMagnitude <= (arrow.transform.position * percentAllowed).sqrMagnitude){
hasHit = true;
}
} else {
Debug.Log ("Has hit, going down");
if ((arrowPositionBeginning - arrow.transform.position).sqrMagnitude <= (arrow.transform.position * percentAllowed).sqrMagnitude){
hasHit = false;
}
arrow.transform.localPosition -= new Vector3 (0.3f, 0.0f, 0.0f);
}
} else {
power = (arrow.transform.position.x - arrowPositionBeginning.x) * 4;
mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.position = Vector3.Lerp (transform.position, mousePosition, power * Time.deltaTime);
arrowShaded.transform.position = arrow.transform.position;
arrow.transform.position = arrowPositionBeginning;
}
}
}
Any help would be greatly appreciated!
Your answer
Follow this Question
Related Questions
Vector3.lerp and then wait 1 Answer
help with returning object to original rotation 2 Answers
Rotation with RotateAround 2 Answers
Usage of InverseLerp 2 Answers