-3

I want to move a sprite using Vector3.Lerp() without StartCoroutine. Starting and target points want to set in the script. I drag & drop the sprite into the Unity Editor and run it. However, the sprite doesn't move. Thanks.

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class MyScript1 : MonoBehaviour {

public Sprite sprite;

GameObject gameObject;  
SpriteRenderer spriteRenderer;
Vector3 startPosition;
Vector3 targetPosition;

void Awake()
{
    gameObject = new GameObject();
    spriteRenderer = gameObject.AddComponent<SpriteRenderer>();        
}

private void Start()
{
    spriteRenderer.sprite = sprite;
    startPosition  = new Vector3(-300, 100, 0);
    targetPosition = new Vector3(100, 100, 0);        
}
void Update()
{        
    transform.position = Vector3.Lerp(startPosition, targetPosition , Time.deltaTime*2f);
}
}
user1232250
  • 329
  • 3
  • 19
  • this question was already marked as duplicate, posting it again wont make any difference.. – Daahrien Jun 04 '17 at 09:34
  • Which part is the same. Are you kidding me? – user1232250 Jun 04 '17 at 09:46
  • @Lestat Can you link the dup? – Noel Widmer Jun 04 '17 at 10:10
  • 1
    https://stackoverflow.com/questions/44351529/unity-how-to-make-a-sprite-move-using-vector3-lerp-without-startcoroutine – Daahrien Jun 04 '17 at 10:28
  • 1
    Possible duplicate of [Unity: How to make a sprite move using Vector3.Lerp() without StartCoroutine](https://stackoverflow.com/questions/44351529/unity-how-to-make-a-sprite-move-using-vector3-lerp-without-startcoroutine) – Hellium Jun 04 '17 at 11:19
  • @Hellium since OP explicitly mentioned 'without StartCoroutine' it's not a duplicate – Bizhan Jun 04 '17 at 12:58
  • @Bijan : Yes it is, because in you read correctly the linked question (even the title), the OP didn't want to use a coroutine either. – Hellium Jun 04 '17 at 13:28
  • Maybe, explain why you don't need coroutine. This is not the right way to use `Vector3.Lerp`. The code in your answer, the one from the doc and the answer that is left on this question are all using `Vector3.Lerp` the wrong way. `Vector3.Lerp` expects `0` to `1` value but those code are giving it more than that. Did I even mention that the movement never ends? It moves *forever*. It could cause permanence problems or a bug that your object cannot be moved to another position because that code is still running. This is why people use coroutine. – Programmer Jun 04 '17 at 14:12

1 Answers1

0

Actually it does move but just a little and only once.

The problem is in lerp method itself: Passing Time.deltaTime*2f as the third parameter is wrong.

The third parameter of lerp method decides a point between startPosition and targetPosition and it should be between 0 and 1. it returns startPosition if 0 is passed and in your case it returns a point very very close to startPosition since you passed a very small number compared to the range (0..1)

I suggest you read the unity docs about this method

Something like this will work:

void Update()
{        
    t += Time.deltaTime*2f;
    transform.position = Vector3.Lerp(startPosition, targetPosition , t);
}
Bizhan
  • 16,157
  • 9
  • 63
  • 101