Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GesterX · Feb 07, 2011 at 05:38 PM · errortransformpositionenemystart

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bob5602 · Feb 07, 2011 at 05:43 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GesterX · Feb 07, 2011 at 05:46 PM 0
Share

Works brilliantly. Thanks for looking at it!

avatar image Bob5602 · Feb 07, 2011 at 05:52 PM 0
Share

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 ins$$anonymous$$d 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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to Return Enemy to startPosition 1 Answer

If gameobject moves do this 1 Answer

Enemy position transform 1 Answer

transform.position error 2 Answers

transporting player to gameobject when it collides with different gameobject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges