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 kaka24fan · Jul 25, 2014 at 04:10 PM · positionspawnchildbulletnoob

Setting a starting position for bullet.

Hi! I'm absolutely new to Unity. I've made few very simple games with Allegro 5 and I know some C++ (just a little!), but C# and Unity are new to me. So please explain everything carefully :) I would be very greatful for both a solution and a thorough explanation. First of all, I've been following this tutorial: http://pixelnest.io/tutorials/2d-game-unity/shooting-2/ I have a WeaponScript attached to both player and enemy prefabs. I want it to detect if my player/enemy has a "barrel tip". If it has I want the shot bullet to spawn at the barrel tip. If it has no barrel tip I want the bullet to spawn where the player/enemy currently is. A "barrel tip" would be a point (gameObject) being in a constant position relative to the player/enemy. So I guess it would have to be its child. But how to code the script? Remember that in the future the player/enemy might have other children and I don't want it to break then. Cheers!

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

1 Reply

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

Answer by robertbu · Jul 25, 2014 at 04:24 PM

An easy solution for this problem is to create a script to use as a marker. Say the script was called 'BarrelTip'. The script does not have to contain anything...perhaps just an empty Start() function. If your character has a barrel tip, then you would add an empty game object at the tip position (or use a visible barrel tip object) and make that object a child of the player. Then you would attach this script. In the code you could do this:

 Vector3 bulletPos;
 BarrelTip bt = gameObject.GetComponent<BarrelTip>();
 if (bt != null)  {
     bulletPos = bt.transorm.position;
 }
 else {
     bulletPos = transform.position;
 }

I'm assuming this code on the player (the one with the transform to use if the barrel tip is not found, and that the barrel tip is a child.

Another solution is to create a variable in your player script:

 public Transform barrelTip;

Then you would initialize this variable only when there is a barrel tip object. If you are building a prefab, you can drag and drop the empty game (or visible) object that holds the barrel tip position on to this variable in the inspector. The code would be:

 Vector3 bulletPos;
 if (barrelTip != null) {
    bulletPos = barrelTip.position;
 }
 else {
    bulletPos = transform.position.
 }

If the barrel tip object will always have the same parent/child relationship with the player or enemy object, another solution is to use Transform.Find() and specify a path to that object. For example, if the barrel tip is always a direct child of the player/enemy, you can do:

 Vector3 bulletPos;
 Transform tr = transform.Find("BarrelTip");
 if (tr != null) {
     bulletPos = tr.position;
 }
 else {
     bulletPos = transform.position;
 }

Assumes that the game object has the name 'BarrelTip'.

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 kaka24fan · Jul 25, 2014 at 04:51 PM 0
Share

Thank you very much for your effort. It's very nice of you to give me as many as three possible solutions. I will try to test all of them eventually. Cheers!

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

2 People are following this question.

avatar image avatar image

Related Questions

Bullets spawn behind plane? 1 Answer

Children Objects move weird 1 Answer

Random spawn points for my target prefabs? 2 Answers

Pretty simple problem: Bullet hole spawn wrong 1 Answer

How do I spawn a prefab to be the child of a mesh? 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