• 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
2
Question by Evil Tebor · Feb 14, 2010 at 01:23 PM · instantiatevelocityparent

Can an instantiated object inherit its parent's velocity?

I have an object which destroys itself then creates a new object at the same time and it works fine. The rotation and everything is all the same but the velocity resets which doesn't really make it seem that seamless. So is there a way i can get the instantiated object to continue with its parent's velocity?

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
7
Best Answer

Answer by duck · Feb 14, 2010 at 02:00 PM

You can set the object's velocity immediately after instantiating it.

In general, you shouldn't use the ability to set velocity directly as a method of controlling or moving the object (you should apply forces instead), but in this situation where you are setting its initial velocity after creating it, it should be fine:

// create the new object var newObject = Instantiate( sourceObject, position, rotation );

// give it the same velocity as the current object newObject.rigidbody.velocity = rigidbody.velocity; newObject.angularVelocity = rigidbody.angularVelocity;

// destroy this object Destroy(gameObject);

You might also find that you need to set up an IgnoreCollision between the old and new objects - if they both have colliders, there's a chance a physics update may occur while they both exist in the same position.

Comment
Add comment · Show 4 · 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 Evil Tebor · Feb 14, 2010 at 02:08 PM 0
Share

Worked brilliantly thanks. :D

avatar image duck ♦♦ · Feb 14, 2010 at 04:19 PM 0
Share

You're welcome - please upvote the answer and click the 'accept' tick to indicate the question has been answered :) Duck 1 $$anonymous$$ ago

avatar image Dubious-Drewski · Sep 27, 2012 at 08:07 AM 0
Share

I have a question.

In my case, I have a non-rigidbody weapon object as a child of my rigidbody player, and my rockets spawn from the weapon. So your solution does not work in its current form.

How do I find the velocity of the root object in a hierarchy?

avatar image Fattie · Sep 27, 2012 at 08:14 AM 0
Share

simply have a variable, boss, that points to the rigidbody

just set it in the editor (ie, drag it)

{ if you don't want to set it in the editor, just use ".parent" to find it in code.

if (for some reason) you truly want to generally find the highest object, search on here for 1000s of questions about that, or ask a new separate question }

then use boss.velocity

avatar image
0

Answer by Atticus052 · Feb 04, 2021 at 03:48 PM

     {
         //Get parent ship velocity
         Vector3 shipVelocity = PlayerShip.GetComponent<Rigidbody2D>().velocity;
         
         //spawn projectile
         GameObject shot = Instantiate(ammoType, firePoint.position, firePoint.rotation);
         
         //create vector3 to store ship velocity
         Vector3 velocity = new Vector3(shipVelocity.x, shipVelocity.y, shipVelocity.z);
         
         //get RB component of bullet
         rb = shot.GetComponent<Rigidbody2D>();
         rb.velocity = velocity;

         //if you want to add force to it, and have it maintain rotation of your parent object
         rb.AddForce(firePoint.up* bulletSpeed, ForceMode2D.Impulse);
         Destroy(shot, bulletLifespan);
     }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Instantiation with inheriting velocity problem 2 Answers

Parenting an instantiated Prefab? 1 Answer

Multiplying something.forward using a variable doesn't actually multiply, but multiplying by a number does. 1 Answer

Instantiated GameObject collision without script repetition? 1 Answer

Change Parent of Multiple Instantiated Objects 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