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 Apollo · Jun 04, 2012 at 07:26 AM · instantiatevelocity

Instantiate with parent/gameObject's velocity

I'd like to drop a projectile and have it share it's parent/gameobject that dropped its velocity. As it stands now, the missile being dropped starts at a velocity of zero (falling quickly behind the player's ship).

There are similar questions to this one on this site, but no answers have worked for me yet : /

var projectile : GameObject;

Instantiate(projectile, transform.position, transform.rotation);

projectile.velocity = GameObject.velocity + speed * transform.forward;

the error i keep getting is:

NullReferenceException: Object reference not set to an instance of an object

I think unity doesn't recognize the phrase "projectile.velocity" but really i have no idea. If some one can tell me how to instantiate a projectile with the velocity of the gameObject which spawned it I'd be very grateful.

I'd settle for preset velocity on an instantiate object, if that's all that can be done..

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 GuyTidhar · Jun 04, 2012 at 07:29 AM

try this:

 // this is the reference to the projectile prefab, from which you will create such
 var projectilePrefab : GameObject;
 
 function CreateProjectile()
 {
    // Make room in memory for new projectile
    var projectile : GameObject;
    // Fill the memory with the data from the projectile prefab
    projectile = Instantiate(projectilePrefab, transform.position, transform.rotation);
    // Set the new projectile with base velocity of 'this' plus speed and direction
    projectile.velocity = gameObject.velocity + speed * transform.forward;
 }
Comment
Add comment · Show 1 · 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 Zarkow · Aug 05, 2015 at 06:12 PM 0
Share

Too bad this no longer work, there is no longer .velocity under a GameObject. Instantiate also returns an Object and have to be cast to a GameObject.

avatar image
0

Answer by Zarkow · Aug 05, 2015 at 06:42 PM

One approach, if you are doing a 2D game:

             GameObject bulletRef = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
             var bulletBody = bulletRef.GetComponent<Rigidbody2D>();
             var v = new Vector2(0f, 10.0f);
             v = Quaternion.Euler(0, 0, m_Rigidbody2D.rotation) * v;
             v += m_Rigidbody2D.velocity;
             bulletBody.velocity = v;

This example assumes you want the projectile to have a base-speed of 10.0f, and want it to fly in the direction of the sprite looking up (think 2D space shooter), and that you also want the projectiles to inherit the full velocity of the shooter.

(Example can be condensed, spread it out a bit to make it more readable.)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help Guitar Hero Style or similar 0 Answers

Velocity and AddForce Problems 0 Answers

Runtime prefab instantiation issue 1 Answer

How can I spawn balls in every direction around a source (making a sphere) and then make the balls move away from that source? 1 Answer

Checking if object intersects? 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