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
2
Question by anothervenue · Dec 02, 2010 at 09:17 PM · instantiateprefabrelative

Lobbing a Bomb with Instantiate.

It seems like this would be a common task in unity, but I can't seem to find an answer.

I would like to instantiate a Bomb (A prefab with its own properties), from my player, lobbing it forward like tossing a ball.

It has to take in to account the players speed too, though the first thing I feel I need to get down is the fact that it will not instantiate relative to its instantiate point, it is instantiating forward along the world Z axis not the relative z of its spawn point.

Here is the basic code that I cant quite get to work

if(Input.GetButtonDown("Powerup") && hasBomb == true)
    {   
        var spawnBomb = GameObject.Find("shootPoint1").transform.position;
        var bomb = Instantiate (bombPrefab,spawnBomb,Quaternion.identity);
        bomb.transform.position = spawnBomb;
        bomb.rigidbody.AddForce (0, 60, 600);
        hasBomb = false;
    }

And then, I would need to add the velocity of the player.

Any help is much, much appreciated.

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 Proclyon · Dec 02, 2010 at 10:01 PM 0
Share

well you could just pretend to be the world axes with an invisible fake object and treat it like some bomb spawning machine.

Epic title/question btw, really sparked my interest :)

avatar image anothervenue · Dec 03, 2010 at 12:43 AM 0
Share

Thanks, thats what the spawnBomb variable is for, it finds "shootPoint1" which is an empty game object, of which my player1 is its parent :)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jayde · Dec 02, 2010 at 09:57 PM

The problem here is that you're adding a force using a vector in global space rather than in local space of your spawn poin.t You can simply use Transform.TransformDirection to convert a vector from local to global space.

if(Input.GetButtonDown("Powerup") && hasBomb == true)
{   
    var spawnBomb = GameObject.Find("shootPoint1").transform;
    var bomb = Instantiate (bombPrefab,spawnBomb.position,Quaternion.identity);
    //It's not necessary to set the position here since you already did in Instantiate
    bomb.rigidbody.AddForce (spawnBomb.TransformDirection(0, 60, 600));
    hasBomb = false;
}

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 Kristov · Dec 02, 2010 at 10:09 PM 0
Share

The only thing I'd add to this is in the line of "var bomb = Instantiate(blah blah); whenever you spawn the bomb, it'd be at it's default rotation, no matter where your character's facing. If you don't want that, change the Quaternion.identity to transform.rotation

avatar image anothervenue · Dec 03, 2010 at 12:45 AM 0
Share

Excellent, thank you this has worked well. Can I ask two more things? Would it be easy to add rotation to the bomb? and how do I add the players current velocity to the direction of the bomb? If I am turbo boosting, the bomb will run into my player.

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

No one has followed this question yet.

Related Questions

Instantiate a prefab with a key press to a relative location? 1 Answer

instantiating vertically 2 Answers

Brain freeze: declaring a prefab inside the same prefab 1 Answer

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Instantiating one prefab with different materials in one frame 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