x


Return Enemy To Start Position

I have a basic script for an enemy to chase a player when the player gets near him. When the player "escapes" the enemy I would like the enemy to return to it's starting position.

I am having a little trouble storing the initial enemy starting position and getting the enemy to return there. The enemy successfully follows the player and stops following when the player gets too far away. But when the enemy is supposed to return to his start position I get an error.

Here is the relevant code (the game is top down view):

var myTransform : Transform; //current transform data of this object private var startPosition;

function Start() { startPosition = transform.position; //store the start position }

function ReturnToPosition() { //rotate to look at the start position myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(startPosition.position - myTransform.position),rotationSpeed*Time.deltaTime);

     //move back to start position
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

}

I have ommitted any code that isn't relevant. I also did a debug to check that ReturnPosition() was being run and it is.

The error I get is:

MissingFieldException: Field 'UnityEngine.Vector3.position' not found.

Thanks for taking a look.

more ▼

asked Feb 07, 2011 at 05:38 PM

GesterX gravatar image

GesterX
2.3k 34 31 62

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

1 answer: sort voted first

Looks like the error comes from this line :

 startPosition = transform.position; //store the start position

Because you're storing your startPosition variable as a Vector3 (transform.position) and then later you're referencing it as startPosition.position. That means what you're really calling is transform.position.position.

I would change your startPosition variable so its declared as something, so do

private var startPosition : Vector3;

then use your startPosition = transform.position

And then in the code, instead of using startPosition.position, just do startPosition.

should do the trick, and cut down some processing time by declaring the variable as an actual type.

more ▼

answered Feb 07, 2011 at 05:43 PM

Bob5602 gravatar image

Bob5602
603 40 33 50

Works brilliantly. Thanks for looking at it!

Feb 07, 2011 at 05:46 PM GesterX

Just another quick note, I noticed your code defines a myTransform : transform. I presume you're using something in the update function to set myTransform = transform each frame. If its all within the same object you can just as easily use transform.position instead of declaring a myTransform and using myTransform.position. Not really a big deal but if you have a lot of objects it can save you some processing power.

Feb 07, 2011 at 05:52 PM Bob5602
(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:

x3617
x2221
x1563
x1051
x240

asked: Feb 07, 2011 at 05:38 PM

Seen: 2909 times

Last Updated: Feb 07, 2011 at 05:44 PM