- Home /
Lerp or MoveTowards not working
I am trying to make a single selected cube move to a certain point. It works fine if I just use transform.position = pointOfImpact, but as soon as I try to use Lerp or MoveTowards, the cube just sits there doing nothing. The frustrating part it, the code worked fine in a different project.
Can anybody see what I'm doing wrong?
var smooth:int;
function Update () {
if(Input.GetKey(KeyCode.Mouse1)){
var mousePosition : Vector3 = Input.mousePosition;
var screenRay : Ray = Camera.main.ScreenPointToRay( mousePosition );
var hitInfo : RaycastHit;
Physics.Raycast( screenRay, hitInfo );
var pointOfImpact : Vector3 = hitInfo.point;
pointOfImpact = Vector3(Mathf.Round(pointOfImpact.x), 0.5f, Mathf.Round(pointOfImpact.z));
transform.position = Vector3.Lerp (transform.position, pointOfImpact, Time.deltaTime * smooth);
}
You are only calling the Lerp if the mouse button is down - did you only want it to move while the button is down?
Looking at your code your multiplying smooth which is just an integer and has no value i don't know if that maybe be the issue of actually stating what value smooth is
Answer by Dracorat · Apr 08, 2013 at 04:21 PM
I believe what you want is more like this: (Please excuse any syntax error - I'm writing off the top of my head)
One movement allowed at a time:
class Something : MonoBehavior {
//This would be attached to the object itself.
private float timeToDestination = 2f; // the f makes the 2 a float
private bool currentlyAnimating = false;
public void Update() {
if(Input.GetMouseButtonDown(0) && !this.currentlyAnimating){
StartCoroutine(MoveThis(Input.mousePosition));
}
}
private IEnumerator MoveThis(Vector3 destinationPosition) {
Vector3 startPosition = this.transform.position;
this.currentlyAnimating = true;
float scale = 1f / this.timeToDestination;
float i = 0f;
while(i < 1f){
i += Time.deltaTime * scale;
this.transform.position = Vector3.Lerp(startPosition, destinationPosition, i);
yield return null;
}
this.currentlyAnimating = false;
}
}
Allow movement to change in the middle
class Something : MonoBehavior {
//This would be attached to the object itself.
private float timeToDestination = 2f; // the f makes the 2 a float
private bool currentlyAnimating = false;
private object threadLock = new object();
private Vector3 destinationPosition;
private Vector3 startPosition;
private float i = 0f;
public void Update() {
if(Input.GetKey(KeyCode.Mouse1)){
bool startNeeded = false;
lock(this.threadLock){
this.i = 0f;
this.startPosition = this.transform.position;
this.destinationPosition = Input.mousePosition;
startNeeded = this.currentlyAnimating == false;
}
if(startNeeded) StartCoroutine(MoveThis());
}
}
private IEnumerator MoveThis() {
lock(this.threadLock){
this.currentlyAnimating = true;
}
float scale = 1f / this.timeToDestination;
while(i < 1f){
lock(this.threadLock){
i += Time.deltaTime * scale;
this.transform.position = Vector3.Lerp(startPosition, destinationPosition, i);
}
yield return null;
}
lock(this.threadLock){
this.currentlyAnimating = false;
}
}
}
$$anonymous$$oveThis should be IEnumerator if coroutine. But still this is a little $$anonymous$$sing me, why using a for loop when a while loop seems way more appropriate? Also, you have a couple of typos like transform destinationPosition should be Transform.
Here is a more simple version:
IEnumerator $$anonymous$$oveThis(Vector3 destinationPosition, float period) {
var t = 0f;
var startingPosition = objectTo$$anonymous$$ove.position;
while(t < 1){
transform.position = Vector3.Lerp(transform.position, destinationPosition, t);
t += Time.deltaTime / period;
yield return null;
}
}
I agree on the while loop so I added it. I already corrected all the rest while you were typing =)
I see one issue yet, I think the OP expects the object to move to the hit point but also to follow if the position changes (chaneg target), hence the use of Get$$anonymous$$ey. Your code will wait for the move to be done before he can start a new move. Or it needs a coroutine cancel logic.
Edit: Actually, I just added the other code I described in detail. Now the OP can choose which he likes.
OP here. Thank you all for replying. I want to selected object to move on a grid like in a top down RPG. Once you select the place to move to, it does not change while moving. The Get$$anonymous$$ey was a mistake. Is the code you gave me in Javaor C#? I don't recognize some of it. (I'm a bit of a coding rookie still).
Your answer
Follow this Question
Related Questions
Leading Reticle 0 Answers
Smooth swapping my game object 0 Answers
3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers