Get Components position and go towards it.

Hello everyone! Im trying to create an object that as soon as the script is run each 2 seconds it will get the position of a GameObject (in this case “Player”). and move towards that position at a certain speed.

If you guys could help me out, I’ve only got this:

public class movementScript : MonoBehaviour {

	public float speed = 2.5f;
        private PlayerController player;
        public Vector3 playerPosition;
	
	// Use this for initialization
	void Start () 
	{
		player = GameObject.Find ("Player").GetComponent<PlayerController>();
	}

       void CheckForPlayerMovement {

            //get Player's Position
            player.position = playerPosition;

           //Move toward that position
       
            transform.translate (playerPosition * speed * Time.deltaTime);
          //I need your help here to do the proper cast of what I want to do,
         //which is move towards playerPosition at a certain speed.
   
            
       }

I also need help with what would be the best practice to call that function at a certain time.(If possible from object creation).

Thanks for your time :smiley:

You need to go through the Unity Scripting Tutorials.

You need to give Translate a direction. You can get the direction by “targetTransform-startingTransform” If you need that normalized (every value between 0&1), there is the normalized property.

You can delay calls to scripts using Invoke and InvokeRepeating

hi, try using vector3.moveTowards Unity - Scripting API: Vector3.MoveTowards