Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 superventure · Dec 04, 2010 at 11:20 AM · transformpositionstartcoordinates

Remember the start coordinates?

I just made a roaming script for some animals, etc. The script currently makes them roam endlessy in any direction and graze/pause for every (x) seconds. I have a lot of animals to place in the level, and would like them to wander for only a certain distance in the range of where ever I happen to place them at.

I'm wondering if there is a way to 'record the start transform.position' so that if they wander too far from it, they respawn(walk) back to that point. Before someone shoves a respawn/waypoint tutorial link down my throat, I KNOW ABOUT SETTING SPAWN POINTS! The thing is, there are many different things I have to place that roam, and manually setting each spawn point for each npc seems entirely unnecessary. It would be far more convenient to just put a script on an npc, place it anywhere in the level, and THAT spot is retained as the 'spawn point' automatically in the script when the game plays. DONE.

here is an idea of the script

private var placed = (coordinates of wherever i dumped you)

if ( Vector3.Distance( placed, transform.position ) >= distance ) { transform.position = placed;

And, I have heard of 'insideUnitCircle' but it does not seem to want to work properly. Here is my best attempt

private var placed: Vector2= Random.insideUnitCircle*2 ;

}if ( Vector3.Distance( placed, transform.position ) >= distance ) {

transform.LookAt(placed);

transform.Translate ( speed* Vector3.forward* Time.deltaTime);

animation.CrossFade("walk");

this works in general, but it makes the npc jump to some far off OTHER random place- no where near where I originally placed the animal.

I know the answer is something simple or right under my nose but I can't seem to sniff out a solution. Please- HELP ME! :p Thanks

Here is part of the code

function Start(){ animation.wrapMode = WrapMode.Loop; animation["walk"].layer = -1; animation["idle"].layer = -1; animation.SyncLayer(-1);

InvokeRepeating ("random",5,5);

//These didn't work- the editor keeps complaining that 'placed' in the update function is undefined. This happens if they vars or just plain var placed = gameObject.GetComponent(Transform); var placed = gameObject.transform; placed = transform.position; }

function Update (){

 if (transform.position.y <=-1){
         transform.position.y =1;


         // this is where the problem is. If I could reference 'placed' from the start function, the npc will go back to wherever 
         // I happen to have dropped it in the level. no extra spawn points or empty game object transforms needed
         }if ( Vector3.Distance(placed.position, transform.position ) >= distance ) {
                      gohome();

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

2 Replies

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

Answer by superventure · Dec 10, 2010 at 11:50 AM

No, actually it be would be

private var placed : Vector3;

but thanks anyway. I believe I was simply overthinking the concept.

Comment
Add comment · 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
0

Answer by Ludovic · Dec 04, 2010 at 02:56 PM

Well, you got the Awake, the Start and the OnEnable methods to do this... See this page to see the differences between these methods Execution Order

BTW, the code you wrote just set the original position somewhere near the origin of the world. You're surely don't want to do that.

var placed:Transform;

function Start() { // Other code

// Set the original transform placed = transform; }

function Update() { // Check out distance here if ( Vector3.Distance(placed.position, transform.position ) >= distance ) { } }

I think you should also check out a tutorial about Unity Javascript like Newbie guide to Unity Javascript or Head First into Unity with JavaScript.

Comment
Add comment · Show 5 · 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 superventure · Dec 04, 2010 at 07:00 PM 0
Share

Well, I tried to put "placed" in the start function (var placed = gameObject.transform;) but I keep getting error messages. The distance detecting part of script is in the update function, and the error is that it complains that 'placed' is an unkown identifier. How do I fix this?

avatar image Ludovic · Dec 05, 2010 at 10:08 AM 0
Share

try gameObject.transform.position. Also did you declared placed outside of the Start and Update functions ?

avatar image superventure · Dec 07, 2010 at 01:40 AM 0
Share

I tried that and got an error message: You are not allowed to call this function when declaring a variable...

avatar image Ludovic · Dec 07, 2010 at 09:20 AM 0
Share

Could you post your script ?

avatar image superventure · Dec 07, 2010 at 05:43 PM 0
Share

I included the part of the script in the question for you. Also, about declaring placed outside of start and update- won't that update the start position otherwise? I put 'placed' in start so that it would NOT update the object's starting transform.position...

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

Finding Coordinates of Objects in a List 1 Answer

Object cannnot be transformed in start() 1 Answer

Transform vector to other coordinate frame using quaternions 1 Answer

Using custom Transform/GameObject classes? 0 Answers

How to Return Enemy to startPosition 1 Answer


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