Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Razor_edge · Jul 11, 2018 at 10:59 AM · scripting problemprefab

Bullet not spawning where it should be,Bullet spawning away from the prefab

Hey guys, new to coding so I will be quick

I got a pistol prefab and a bullet prefab. I got my gun prefab to instantiate onto my player model as a child of the hand no problem, however, whenever I shoot my bullets it does not come out where I have the spawner (Which is a child of the pistol prefab) but instead the area the spawner was before I made the pistol prefab and destroyed the original pistol. No matter where I move or how I turn, it will always spawn in the same area going in the same direction. Here is the code for the shooting on the actual pistol:

 using UnityEngine;
 using System.Collections;
 
 public class pistol_controls : MonoBehaviour {
 
     public GameObject bullet_fixed;
     public Transform pistol_spawner;
 
     public void fire()
     {
         var bullet = (GameObject)Instantiate(bullet_fixed, pistol_spawner.transform.position, pistol_spawner.transform.rotation);
         Rigidbody temp_rigidbody;
         temp_rigidbody = bullet.GetComponent<Rigidbody>();
         temp_rigidbody.AddForce(transform.forward * 20);
     }
 }

Anything I did wrong?

UPDATE: An example of the exact problem I am having

So locally to the pistol, say that the spawner is 10, 10, 10 (As an example). The bullets spawn globally at 10, 10, 10 every single time. If I change the local position of the spawner compared to the pistol to 5, 5, 5 the bullets spawn at 5, 5, 5 globally

Comment
Add comment · Show 8
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 FChavez509 · Jul 11, 2018 at 10:57 PM 0
Share

I'm sorry, but I have to ask. Is the spawner a child of the original pistol or a child of the instantiated pistol?

You code looks correct to me at first glace, so having the wrong pistol_spawner reference would account for your description of the issue.

avatar image Razor_edge FChavez509 · Jul 12, 2018 at 09:49 AM 0
Share

The spawner is a child of the original pistol which I then made the prefab out of

avatar image TreyH · Jul 12, 2018 at 01:00 PM 0
Share

Can you post a screenshot of your hierarchy so that we can recreate your setup? As written, you should be spawning things at their global positions as you're using global positions as arguments for the Instantiate call.

Is there anything else that might be influencing your bullet's position? Are you maybe moving the bullet in a weird way with a script?

avatar image Strixie13 · Jul 12, 2018 at 01:02 PM 0
Share

I think the issue is the pistol_spawner variable being used as a transform. Basically when you dragged and dropped the pistol position it just took that place in world space and saved it as a variable. Try re-doing it with the actual whole pistol spawner object set to the pistol_spawner variable (even if the only component is a transform), and then access the transform like you did with pistol_spawner.transform.position (this will now give the local position of the game object relative to the parent, rather than a global transform in world space). public GameObject pistol_spawner ins$$anonymous$$d.

avatar image TreyH Strixie13 · Jul 12, 2018 at 01:06 PM 0
Share

Not sure, Transform and GameObject are classes, making them reference types. All the inspector assignment does is tell Unity that this instance of pistol_controls will start with a reference to whatever Transform instance OP assigned in the editor.


This would be the case if that was a Vector3 or some other value type, though.

avatar image FChavez509 · Jul 12, 2018 at 10:53 PM 0
Share

After reading your update, this problem stopped making sense to me.


When you say locally to the pistol, do you mean as a child of the pistol? Because the code you are showing us, is the world-position of the pistol_spawner. For that to match up its local-position, the pistol_spawner would either need to be parentless, or its parent's local-space would need to match up to world-space. The reason I'm confused is because I'm reading your problem as if the pistol_spawner is a child of the pistol GameObject, but your description of how the coordinates are acting is making it sound like it isn't a child of the pistol GameObject.

avatar image Razor_edge FChavez509 · Jul 13, 2018 at 05:25 AM 0
Share

I've checked while the game is running, the spawner is definitely a child of the pistol when the pistol itself is added to the game.

avatar image yaroslav_ · Jul 13, 2018 at 05:02 AM 0
Share

have you tried transform.localPosition = vaector3.zero

6 Replies

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

Answer by Razor_edge · Jul 13, 2018 at 05:43 AM

I FIGURED IT OUT!!!!!

The problem was that the character script was calling the firing function of the prefab and NOT the function of the instantiated gun, therefore it spawns where I placed the prefab before I put into my project files NOT where the instantiated gun is. I feel so stupid figuring this out but baby steps.

THANK YOU TO ANYONE THAT ASSISTED ME.

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

Answer by bner2008 · Jul 11, 2018 at 11:46 AM

Hello, I suggest you check if your prefabs default position/rotation values are set to 0, not the instance of the prefabs but the prefabs themselves. It's easy to forget that and that can lead to a lot of issues if you try to use their position because it won't be precisely where you'd think they'd be.

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 Razor_edge · Jul 11, 2018 at 11:58 AM 0
Share

It wasn't set to 0, 0, 0... so thanks for re$$anonymous$$ding me that. However, it still isn't spawning where I want it. Thanks for the suggestion though.

avatar image SwixDevs · Aug 24, 2020 at 06:48 AM 0
Share

Thanks a lot for the answer, it works perfectly!

avatar image
0

Answer by Strixie13 · Jul 11, 2018 at 09:56 PM

I think the error is on the last line. It says "transform.forward" which would use the transform of the game object the entire script is attached to instead of the spawner object. Try temp_rigidbody.AddForce(pistol_spawner.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 Razor_edge · Jul 12, 2018 at 05:17 AM 0
Share

No, sorry man, still spawning not where I need it. I should make a note that the bullets spawn in the exact same place in the global transformation and it is linked to the local translation of the spawner to the gun. It's odd. Thanks anyway.

avatar image
0

Answer by kodierer · Jul 11, 2018 at 10:20 PM

Maybe try transform.localPosition transform.localRotation or even setting the local positions to 0 in code. transform.localPosition.x=0 etc..

As a hack, you may try seeing how far away things are spawning, and subtracting the difference.

@Razor_edge

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 Razor_edge · Jul 12, 2018 at 05:20 AM 0
Share

Tried this and didn't work again. It isn't that the bullets spawn away from the spawner and changes globally when I move, I mean that the bullets spawn in the same place globally but using the local space the spawner is compared to the pistol, if that makes sense.

So locally to the pistol, say that the spawner is 10, 10, 10 (As an example). The bullets spawn globally at 10, 10, 10 every single time. Thanks for the recommendation though

avatar image
0

Answer by AdityaViaxor · Jul 12, 2018 at 11:36 AM

Actually just comment the last line then see position because when it instantiate you forward it by 20 unit so try commenting it.

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 Razor_edge · Jul 12, 2018 at 11:56 AM 0
Share

Thanks very much for the suggestion, though that's not the solution.

That line works only to propel the bullet forward after it instantiate, which works well, it is just spawning in the wrong position. I've narrowed the problem down:

The script for some reason is using the local transformations of the spawner object as the global transformations to use the bullet with, so like I said at the update, if the spawner was located 10, 10, 10 on the actual pistol itself, the bullets start spawning at 10, 10, 10 in the global scope of the scene.

  • 1
  • 2
  • ›

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

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

Related Questions

Tower defense target update 0 Answers

Card Game like yu-gi-oh 1 Answer

rb.AddForce (transform.forward * BulletForce) has issues. 1 Answer

Muzzle Flash Prefab 1 Answer

How to get InputField Text from prefabClone? 2 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