1

I want to move an instantiation of a rigidbody2d(Ship) from one point to another.
Shippos is my point a.
Targetpos is my point b.
They are assigned in the inspector, The instantiation is triggered by a mouse click on object,
My problem is i can't figure out the right function to use ,Or where to put it to be able to move my instance ,Or how to control the speed of the movement.

public class Attack : MonoBehaviour {

public Transform ShipPos;
public Transform Targetpos;
public Timer temp;
public Rigidbody2D ship;
public float speed = 4;
public float step; 

void start(){

}
// Update is called once per frame
void Update () {


}
void OnMouseDown(){

    step = speed * Time.deltaTime;
        temp.timeleft /= 2;
        for (int i = 0; i < temp.timeleft; i++) {
            Rigidbody2D shipinstance;
            shipinstance = Instantiate (ship, ShipPos.position, ShipPos.rotation) as Rigidbody2D;

        }



}

}

Makdous
  • 23
  • 9
  • Start a coroutine when the button is pressed then pass that instantiated object to the `moveToX` function in the duplicate. – Programmer Apr 21 '18 at 11:01
  • i can't put the instance in the other function out of scope – Makdous Apr 21 '18 at 11:20
  • `Transform obj = Instantiate (ship, ShipPos.position, ShipPos.rotation) as Rigidbody2D;` then pass `obj` to the function: `StartCoroutine(moveToX(obj , pointB, 1.0f));` Nothing complicated here. – Programmer Apr 21 '18 at 11:32
  • I tried it and it worked but only for one of my instance how can i make them all move at the same time(i make more than one instance at a press) – Makdous Apr 21 '18 at 11:51
  • Remove the `isMoving` variable and any code that uses it in that function – Programmer Apr 21 '18 at 11:53

0 Answers0