Question by
test19user66 · Dec 18, 2018 at 09:42 PM ·
2d game2d spritesmovetowards
Unity 2D movetowards target error
Hi guys, i'm newbie in Unity3D. i want to move my objects to target. if objects hit collider until reach target, my objects need to come back(old position) for new target.
Here is my current code;(no errors but not working)
public float speed = 1.0f;
public Transform MyPos;
public Transform Target;
// Use this for initialization
void Start () {
StartCoroutine( Move() );
}
public IEnumerator Move()
{
yield return new WaitForSeconds( 2.0f );
MyPos.position = gameObject.transform.position;
Debug.Log (MyPos.position);
}
void Update () {
Target = GameObject.FindGameObjectWithTag ("target").transform;
Debug.Log ("log2");
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, Target.position, step);
Debug.Log ("log3");
}
Comment