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 wegaz · Aug 19, 2014 at 06:58 PM · javascripttransformpositionyield

Changing object location every 3 second

I am working on very simple slender man game. The problem is my location change code works really weird.

Here is the code:

 #pragma strict
 var Spawn1 : Transform;
 var Spawn2 : Transform;
 var Spawn3 : Transform;
 var Spawn4 : Transform;
 
 function Update () 
 {
 spawn();
 }
 
     function spawn ()
     {
     yield WaitForSeconds (3);
     this.transform.position = Spawn1.position;
     yield WaitForSeconds (3);
     this.transform.position = Spawn2.position;
     }
Comment
Add comment · Show 4
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 Landern · Aug 19, 2014 at 07:07 PM 0
Share

i'm not sure what really weird means. Perhaps you can explain exactly what is happening or what you find weird about your currently solution.

avatar image wegaz · Aug 19, 2014 at 07:24 PM 0
Share

idk how to explain that but it is really weird. If you are avaliable to test that on your own unity you will see that somethings are really wrong at this script.

I am really sorry but my english isnt enough to explain that. Actually I am not sure about I can explain this even in my native language.

avatar image Landern · Aug 19, 2014 at 07:36 PM 0
Share

okay, hows this, what is this script attached to? Now also remember, the code above will execute once per frame, stacking 3 second calls and yielding during the entire game play.

avatar image wegaz · Aug 19, 2014 at 07:43 PM 0
Share

It can be attached to any obect. $$anonymous$$y goal with this script is;

It will change the location of object every 3 seconds with this line:

Spawn1

wait 3 seconds

Spawn2

wait 3 seconds

Spawn 3

Wait 3 seconds

Spawn 4

and then again spawn 1, spawn 2, spawn 3, spawn 4.

Actually I tried to make a loop.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Aug 19, 2014 at 08:16 PM

Try this(it was not tested), assign the Transforms in the inspector.

 import System.Collections.Generic; // To use list
 
 #pragma strict
 var spawnPositions : List.<Transform> = new List.<Transform>(); // Add as many as you want in the inspector
 var spawnIndex : int = 0;
 
 function Update () 
 {
   // If not invoking, invoke the spawn method.
     if (!IsInvoking("spawn"))
         Invoke("spawn", 3); // Invoke every 3 seconds if not invoking already
 }
  
 function spawn ()
 {
     this.transform.position = spawnPositions[spawnIndex].position; // Set this gameobjects transform to the spawnIndex of spawnPositions
     
     spawnIndex++; // Increment the currentIndex
     
     // The index is 0 based when accessing items in the list, we need to compare the count minus 1 to see if we need to reset the spawnIndex.
     if (spawnIndex > spawnPositions.Count - 1)
         spawnIndex = 0;
 }
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 wegaz · Aug 20, 2014 at 03:27 PM 0
Share

Error

Here is the error that I am getting ^

error.png (210.9 kB)
avatar image Landern · Aug 21, 2014 at 03:30 PM 0
Share

Sorry, i made a mistake on the list declaration.

avatar image
0

Answer by Cherno · Aug 20, 2014 at 03:33 PM

 var t : float;
 var threshold : float = 3.0f;
 var newPos : Vector3;
 
 function Update() {
 
      t += Timer.deltaTime;
      if(t >= threshold) {
           t = 0.0f;
           transform.position = newPos;
      }
 
 
 }

How you determing the next position is up to you; you could use

 newPos = new Vector3(Random.Range(0,100), 0, Random.Range(0,100));

so it randomly pikcs a ground position between 0 and 100 and the x and z axis.

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

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turning character slowly with Slerp 1 Answer

distance to variable object 1 Answer

Instantiate as a child at position 2 Answers

Position of a GameObject 2 Answers

Locating Position of Object not Working? 2 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