Make Object Move to/follow Another Object? (plus turn towards it)


I am trying to make a simple game where objects close in on an other object (you!).

I am having them spawn all around the character in a random location, so I will not be able to know where they are.

What I want to be able to do is have a script that will have a speed variable controlling how fast they come towards you.

I suck at scripting though, and I am even worse at scripting the movement of objects in 3D space.

I would also like the script to be compatible with following a character that moves.
I was hoping some nice scripting guru could give me a script or lead me in the right path.

I saw something once a long time ago, a very complex Youtube tutorial, to doing what I want, but I can’t find it plus it was in C# which I am trying to avoid, as I use Javascript.

I would really appreciate some help!



EDIT: I just remembered that the script will also need to have the object turn so it is always facing towards you. The objects are pieces of text, and I can't see some of them because they are either reversed or I am seeing the side.

2nd EDIT:

Thank you for your responce. Now I need a second script that will turn it forward facing the target. Anyone?


Look in to the Vector3.moveTowards() function. it takes a current position, a target position and a float to determine the rate at which your object moves.

function Update(){
    transform.position = Vector3.MoveTowards(transform.position, targ.transform.position, .03);

Also Lerping could do it for you

function Update () {
    transform.position = Vector3.Lerp(start.position, end.position, Time.time);

I have some thing similar i’m working on but i C# could you take a look at it. it doesn’t seem to work.
using UnityEngine;
using System.Collections;

public class FollowAI : MonoBehaviour {

    public GameObject target; //the enemy's target
    public float moveSpeed = 5; //move speed
    public float rotationSpeed = 5; //speed of turning
    private Rigidbody rb;
	private Vector3 mytransform = target;

    void Start()
        rb = GetComponent<Rigidbody>();

   void Update()
        //rotate to look at the player
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        transform.position += transform.forward * Time.deltaTime * moveSpeed;