I’m trying to get the following gameobjects to move towards another (my player), the only problem is with the following code, they only move towards its starting position. Where as I want them to be pulled towards where ever the player currently is. Any help would be appreciated.
public Transform player;
float speed = 5.0f;
void Update () {
if (PlayerScript.pulltime > 0)
transform.position = Vector3.MoveTowards(transform.position, player.position, speed*Time.deltaTime);
}
The Players movement script is as follows:
void moveship ()
{
if (Input.GetKey (KeyCode.UpArrow))
moveshipUp ();
if (Input.GetKey (KeyCode.DownArrow))
moveshipDown ();
if (Input.GetKey (KeyCode.RightArrow))
moveshipLeft ();
if (Input.GetKey (KeyCode.LeftArrow))
moveshipRight ();
}
void moveshipUp ()
{
if (this.transform.position.y < 3.4) {
this.transform.Translate (Vector3.up * speed * Time.deltaTime, Space.World);
}
}
void moveshipDown ()
{
if (this.transform.position.y > -4.388) {
this.transform.Translate (Vector3.down * speed * Time.deltaTime, Space.World);
}
}
void moveshipLeft ()
{
if (this.transform.position.x < 8.23) {
this.transform.Translate (Vector3.right * speed * Time.deltaTime, Space.World);
}
}
void moveshipRight ()
{
if (this.transform.position.x > -8.284) {
this.transform.Translate (-Vector3.right * speed * Time.deltaTime, Space.World);
}
}
It should be following it’s updated movement, but it doesn’t.