2

How can I move/animate/translate/tween an Image from position A to position B using C# code in Unity 4.6?

Assuming Image is a GameObject, so it could be a Button or whatever. There has to be a one-liner for this, right? I've been googling for a while but all I can see out-of-the-box is stuff done in Update, and I firmly believe doing stuff in Update is not a fast way of scripting things.

maZZZu
  • 3,585
  • 2
  • 17
  • 21
Jonny
  • 15,955
  • 18
  • 111
  • 232

2 Answers2

2

maZZZu's method will work, however, if you do NOT want to use the Update function, you can use an IEnumerator/Coroutine like so…

//Target object that we want to move to
public Transform target;
//Time you want it to take before it reaches the object
public float moveDuration = 1.0f;

void Start () 
{
    //Start a coroutine (needed to call a method that returns an IEnumerator
    StartCoroutine (Tween (target.position));
}

//IEnumerator return method that takes in the targets position
IEnumerator Tween (Vector3 targetPosition)
{
    //Obtain the previous position (original position) of the gameobject this script is attached to
    Vector3 previousPosition = gameObject.transform.position;
    //Create a time variable
    float time = 0.0f;
    do
    {
        //Add the deltaTime to the time variable
        time += Time.deltaTime;
        //Lerp the gameobject's position that this script is attached to. Lerp takes in the original position, target position and the time to execute it in
        gameObject.transform.position = Vector3.Lerp (previousPosition, targetPosition, time / moveDuration);
        yield return 0;
        //Do the Lerp function while to time is less than the move duration.
    } while (time < moveDuration);
}

This script will need to be attached to the GameObject that you would like to move. You will then need to create another GameObject in the scene that will be your Target…

The code is commented but if you need clarification on something just post a comment here.

Savlon
  • 744
  • 2
  • 11
  • 18
1

If you want to do the movement yourself you can use something like this:

public Vector3 targetPosition = new Vector3(100, 0, 0);
public float speed = 10.0f;
public float threshold = 0.5f;
void Update () {
    Vector3 direction = targetPosition - transform.position;
    if(direction.magnitude > threshold){
        direction.Normalize();
        transform.position = transform.position + direction * speed * Time.deltaTime;
    }else{ 
        // Without this game object jumps around target and never settles
        transform.position = targetPosition;
    }
}

Or you can download for example DOTween package and just start the tween:

public Vector3 targetPosition = new Vector3(100, 0, 0);
public float tweenTime = 10.0f;
void Start () {
    DOTween.Init(false, false, LogBehaviour.Default);
    transform.DOMove(targetPosition, tweenTime);
}
maZZZu
  • 3,585
  • 2
  • 17
  • 21
  • 1
    The latter way using a 3rd party library is what I'm leaning against (and currently am already working on), but I'm still puzzled by the lack of such features in Unity itself. Doing stuff like this takes only one line of code in Cocos2D and even native SpriteKit of iOS. 10+ lines vs 1 line just doesn't make sense. Maybe a good question is "What kind of workflow would benefit from a very code centric update-only way of animating things?". Seeing as Unity is supposed to be easy and GUI centric. – Jonny Nov 25 '14 at 08:10