Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 smirlianos · Sep 11, 2015 at 07:37 PM · rigidbodyvelocityspeed

instantiated objects have different speed

Hello,

I have a spawner object and I want to Instantiate an enemy at a given time. This enemy (a prefab) has a linear movement, with a specified speed, set in the inspector. The problem is that when the enemy spawns, sometimes his speed is higher than normal, or slower... Why is that?

Spawner script:

 #pragma strict
 InvokeRepeating("Spawn", 2, Random.Range(1,5)); //We spawn an enemy between 1 to 5 seconds
 
 var dire : int; //The direction the enemy follows (left or right, set in the inspector)
 var en : GameObject; //The enemy game object;
 
 
 function Spawn () {
     var enemyIn = Instantiate(en, transform.position, transform.rotation);
     enemyIn.GetComponent(Enemy).dir = dire; //set the enemy direction
 }

Enemy Script:

 #pragma strict
 var dir : int; //the direction of the enemy, set by the spawner script
 var speed : float; //the speed of the enemy, set on the inspector
 
 function Start () {
     rigidbody2D.velocity.x = dir * speed * Time.deltaTime;
     yield WaitForSeconds(5);
     Destroy(gameObject); //we destroy the gameobject for memory reasons
 }



The enemy rigidbody is set to "Fixed Angle" and Gravity Scale = 0. Everything else is default.

Comment
Add comment · Show 2
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 jmgek · Sep 11, 2015 at 08:50 PM 0
Share

You may be colliding with the the spawner object and this could effect it, does your spawner manager "script" have a collider attached to it?

avatar image smirlianos jmgek · Sep 11, 2015 at 09:46 PM 0
Share

No, the spawner is an empty game object. Also, if I pause the game and check the insantiated objects, they all have the same speed in the inspector, even if they are moving at different speeds. So I believe there is a physics problem involved

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by jmgek · Sep 11, 2015 at 10:04 PM

Okay so if that's not the issue I looked at your code and Time.deltaTime should be in an update function and not in the start function. Because you are assigning your objects velocity at the start you don't need to update the time and the time that you are multiplying is actually equal to the actual fractions of seconds it takes for the frame to calculate.

  #pragma strict
  var dir : int; //the direction of the enemy, set by the spawner script
  var speed : float; //the speed of the enemy, set on the inspector
  
  function Start () {
      rigidbody2D.velocity.x = dir * speed;
      yield WaitForSeconds(5);
      Destroy(gameObject); //we destroy the gameobject for memory reasons
  }

alternativly if you wanted you could use this:

 function Update () {
         // Move the object 10 meters per second!
         var translation : float = Time.deltaTime * 10;
         transform.Translate (0, 0, translation);
     }

I may be wrong so let me know if this worked.

Comment
Add comment · Show 3 · 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 jmgek · Sep 11, 2015 at 10:09 PM 0
Share

Also, "we destroy the gameobject for memory reasons" is not entirely true. to destroy an object it takes more processing memory to rid the memory from the vram so in that case I would just pool the objects seeing how you may be concerned about memory or want best practices. Or deactivate the objects ins$$anonymous$$d of destroy, seeing how you "Could" have a large number of objects being destroyed at the same time, (one in a million case where you have 40 objects being destroyed on the same frame) this will drop your frames. https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

avatar image TechTayyab · Apr 19, 2020 at 05:15 PM 0
Share

I would have regarded you the best answer if I could. $$anonymous$$y problem was pending from 2 days and this answer solved it. Thanks :)

avatar image mr_noodler · Dec 02, 2021 at 05:38 AM 0
Share

Thank you jmgek for this!! I was having the exact same problem with a fresh install of Unity 2021!!!

Very helpful!!

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

29 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Keeping momentum with rigid bodies. 0 Answers

Linear Velocity 1 Answer

Comparing rigidbody speeds 1 Answer

How Do I Increase the ForwardForce of the player over time? using rigid body 2 Answers

Rigidbody speed / Velocity issue 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