x


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

Hello.
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!
:P
Thanks!!

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?
Thanks!

more ▼

asked Apr 03 '11 at 09:54 PM

Keavon gravatar image

Keavon
358 30 34 44

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Apr 03 '11 at 10:57 PM

Kobby gravatar image

Kobby
48 5 5 9

Hi. Thank you for your answer. The problem is that this script does not turn the gameobject to face forward towards the specified target. Any ideas on a second script to do that? Thanks!

Apr 04 '11 at 03:30 AM Keavon

sorry I forgot to respond to the edit. you can also call a transform.lookAt() that will keep your objects looking at 1 game object all the time.

private var targ: GameObject;

function Update(){ transform.LookAt(targ.transform); }

Apr 04 '11 at 02:38 PM Kobby

sorry for the unformatted code

Apr 04 '11 at 02:38 PM Kobby
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1120
x515
x469
x319
x37

asked: Apr 03 '11 at 09:54 PM

Seen: 7058 times

Last Updated: Apr 04 '11 at 02:24 PM