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 kamuzai1226 · Jul 16, 2014 at 08:03 AM · 2dmovementdistancereuse

Move objects infinite based on distance and not an invoke

So I kind of hit a wall with my logic and I need some help trying to flesh out this idea.

Currently I have an invoke that gets an object from an object pool and basically spawns it every second and it moves automatically. What I found is that when you increase the speed of the object, the distance between the objects spawning is increased, which is not the desired result. What needs to happens it that the distance between the spawned objects will remain the same no matter the speed of the objects, it will always spawn 10 units away from the next.

Clearly this bit of code is just terrible and is not at all reusable and only works once through.

void Start() { obj1 = GetPooledObject(); // get an inactive object obj1.SetActive(true); //set that inactive object to active. This begins object movement }

 void Update()
 {
     distance = Vector2.Distance(obj1.transform.position, transform.position); //calculate distance between this object and objectpool start position
         
     if (distance > 10 && !secondObject)
     {
         MoveSecondObj();
     }
 }
 
 private void MoveSecondObj()
     {
         var obj2 = GetPooledObject();
         obj2.SetActive(true);
         secondObject = true;
     }

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

Answer by Kiwasi · Jul 16, 2014 at 08:30 AM

Essentially you are describing a wave. The nature of waves is that speed = frequency * wavelength. If you increase speed you have to decrease frequency to get the same wavelength. In other words call your spawn function more often.

Invoke repeating is not well suited for this, use a coroutine or a custom timer instead.

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 smallbit · Jul 16, 2014 at 08:33 AM

If you dont need to use the obj1 for anything more than spawning procedure, you can use it to store the reference of your last spawned object. Than In Update you check its distance to the spawner (Transform.position) if its bigger than 10, you spawn another object and put its reference to obj1. This is simple code that could do that ( I assume that GetPooledObject() returns Transform type. )

 Transform obj1;
 void Start()
     {
        SpawnNextObject();
     }
     
     void Update()
     {
         distance = Vector2.Distance(obj1.transform.position, transform.position); //calculate distance between this object and objectpool start position
             
         if (distance > 10)
         {
             SpawnNextObject();
         }
     }
   
     void SpawnNextObject()
     {
             obj1 = GetPooledObject(); // get an inactive object
             obj1.SetActive(true); //set that inactive object to active. This begins object movement
 
     }    

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 Kiwasi · Jul 16, 2014 at 09:08 AM 0
Share

Calculating distance each frame is expensive. This answer could be more efficient if you use Vector3.sqr$$anonymous$$agnitude ins$$anonymous$$d.

avatar image kamuzai1226 · Jul 16, 2014 at 04:34 PM 0
Share

Thanks guys, this really helped.

Also, how do I find out what is expensive and what isn't expensive?

avatar image kamuzai1226 · Jul 16, 2014 at 06:11 PM 0
Share

So now there is a problem of inconsistent spacing of when the object spawns. I think this is a problem with inconsistent fps, therefore the distance can be inconsistent to the point where it is very noticeable to the player.

How would I go about fixing this problem?

avatar image Kiwasi · Jul 16, 2014 at 06:42 PM 0
Share

How inconsistent is your FPS?

You could always adjust your spawn position exactly to suit based on the actual distance. But fixing your fps issues would be a better option.

The docs normally indicate weather something is expensive or not, or if there is a cheaper way. As a general rule anything with square roots should be avoided if possible.

avatar image kamuzai1226 · Jul 16, 2014 at 06:54 PM 0
Share

So, it may or may not be an fps issue because it is always 84+, but what I think it might be is that Vector2.Sqr$$anonymous$$agnitude is executing the SpawnWall() too soon and I am unsure why. It is very erratic.

For it to start, I call SpawnWall() from another script and by doing that, it will set the rest of the Spawning script into motion.

 void Update ()
 {
     if (spawnedWall)
     {
         distance = Vector2.Sqr$$anonymous$$agnitude(spawnedWall.transform.position - transform.position);
         if (distance >= wallSpacing*wallSpacing)
         {
             SpawnWall();
         }
     }
 }
 
 public void SpawnWall()
 {
     spawnedWall = GetPooledObject();
     spawnedWall.transform.position = transform.position;
     spawnedWall.SetActive(true);
 }  

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D one distance at a time movement 2 Answers

Uneven speed in 2d movement script 2 Answers

Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers

Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer

How do I prevent characters from being able to land on each others heads? 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