Hello everyone. I want to make it so that when a gameObject (“Player”) controlled by the player moves, a second one (“Secondary”), after a brief delay, starts moving from a set distance, following it, such that the distance between the two remains the same after the second gameObject stops. The idea is that Secondary will always go to a point y = ([current Player position] - 1.9) even after Player stops moving.
These are the two C# scripts I used, however, the second one only makes the second gameObject appear at the set distance after the delay, without displaying movement. How could I fix this, so that Secondary is shown moving? Thank you in advance.
First script:
public class PlayerMovement : MonoBehaviour {
public float speed = 1f;
void Start () {
}
void Update () {
if (Input.GetKey (KeyCode.D))
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
if (Input.GetKey (KeyCode.A))
transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
if (Input.GetKey (KeyCode.W))
transform.position += new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
if (Input.GetKey (KeyCode.S))
transform.position -= new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
}
}
Second script:
public class SecondaryMovement : MonoBehaviour {
Transform pos;
public float timer = 0;
public float delay = 1f;
void Start () {
pos = GameObject.Find("Player").transform;
}
void Update () {
timer -= Time.deltaTime;
if (timer <= 0) {
transform.position = new Vector3 (pos.position.x, pos.position.y - 1.9f, transform.position.z);
timer = delay;
}
}
}