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 Bishmanrock · Feb 18, 2021 at 09:05 PM · scripting probleminstantiateinstantiate prefabinstantiation

Instantiated Bullets Not Picking Up Correct Position/Rotation

I've been trawling through here and Google for the last three hours to no avail, so hoping I can get some help before I pull my hair out.

Recently instantiating the projectiles of my weapon has broken. I don't know when this occurred, so I can't retrace my steps.

The current projectile instantiation script is:

 Projectile newProjectile = Instantiate(projectilePrefab, weaponMuzzle.position, weaponMuzzle.rotation);



The weaponMuzzle is an empty container on the weapon specifically to spawn projectiles. It was originally child to the barrel, but I've moved it to the parent layer for testing. No difference has been observed. The weapon itself sits in a container under the player object when equipped, and the container (and therefore everything under it) is rotated according to the camera view. I don't know if the fact that the weapon may not have its own rotation because it's a child may be affecting anything, but I'm certain it was the same way before and worked.

The bullet instantiates in front of the player, but always as if they're looking straight ahead (e.g. the bullet picks up the correct direction, but doesn't pick up if the gun is aimed up or down (as in it doesn't pick up the variations on the y axis) and doesn't pick up the aimed angle (so aiming up will produce a bullet that shoots straight forward).

That feels quite wordy so here's an amazing diagram:

alt text

I've used Quaternions and eulerAngles to no avail. I've also tried just stripping out the rotation aspect completely just to try and get the bullet to line up with the gunMuzzle, but I can't seem to get that to work, suggesting to me this isn't just a rotation issue.

I feel like I'm overlooking something really simple, can someone put me out of my misery?

Thanks in advance

EDIT: Troubleshooting I've tried

  • Instantiating at weaponMuzzle.parent.position produces the same result

  • Instantiating, and then setting the transform.position and transform.eulerAngles as a later step (to rule out anything else resetting the position) does not resolve

  • Setting the instantiated objects Quaternion to transform.eulerAngles.x/y/z does not seem to do anything

  • Instantiating as a child and then immediately setting the newProjectile.transform.parent to null does not resolve.

  • I passed through the players camera object and used that to set the position/rotation of the instantiated projectile as I suspected the child objects were not seeing this, but the same issue is occurring.

I feels to me like it can't get the proper information from the object it's instantiated from anymore?

EDIT2: Further steps tested:

  • Creating an instance as a child and then immediately removing the link gives same results. Code used:

           Projectile newProjectile = Instantiate(projectilePrefab, weaponMuzzle.position, weaponMuzzle.rotation, weaponMuzzle.transform);
             newProjectile.transform.SetParent(null);
    
    



- Going back to the original script isn't working anymore for some reason. Original script on the weapon:

         Vector3 shotDirection = GetShotDirectionWithinSpread(weaponMuzzle);
         Projectile newProjectile = Instantiate(projectilePrefab, weaponMuzzle.position, Quaternion.LookRotation(shotDirection));
         newProjectile.Shoot(this);



Corresponding code on the Projectile:

     public void Shoot(WeaponController controller)
     {
         owner = controller.owner;
         initialPosition = transform.position;
         initialDirection = transform.forward;
         initialDirection = transform.forward;
         inheritedMuzzleVelocity = controller.muzzleWorldVelocity;
         initialCharge = controller.currentCharge;
 }



  • Also discovered that if the WeaponFX when shooting isn't childed to the weapon, it suffers from the same issue, so there must be a problem with my overall logic.

Still feel like I'm missing something basic. Done a lot of reading up about Instantiation and it all makes sense and I can get working, so I'm assuming something else is conflicting with this.

[1]: https://learn.unity.com/project/fps-template

project.png (10.2 kB)
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 AbandonedCrypt · Feb 19, 2021 at 09:39 AM 0
Share

Are you setting the parent in code?

avatar image Bishmanrock AbandonedCrypt · Feb 19, 2021 at 09:45 AM 0
Share

As it's a projectile I haven't been spawning it with a parent, should I be?

When I was testing I did spawn it with the weaponMuzzle as the parent, which then picked up the correct location/angle but kept its movement with the gun after firing, so if the player turned so did the bullet

avatar image AllTheGoodNamesWereTaken · Feb 19, 2021 at 07:55 PM 1
Share

I haven't done the Unity FPS Microgame but my guess is that the bullet is coded to just move forward in what ever direction it is facing. It sound like you can get it to fire in what ever direction the gun in facing in a circle but not up or down. This means the bullet is simply not facing up or down correctly when it is initiated. I notice you have 2 lines in your Shoot function that say the exact same thing: initialDirection = transform.forward; Is that suppose to be that way?

avatar image Bishmanrock AllTheGoodNamesWereTaken · Feb 19, 2021 at 08:46 PM 0
Share

I notice you have 2 lines in your Shoot function that say the exact same thing: initialDirection = transform.forward; Is that suppose to be that way?

As part of testing I've actually disabled everything in the bullet object, so I'm just concentrating on instantiating the bullet at the right position/rotation for now. The issue still persists, so I know it's nothing on the bullet script now at least.

It sound like you can get it to fire in what ever direction the gun in facing in a circle but not up or down.

That's correct, but also when the gun is pointed upwards the bullet doesn't instantiate higher/lower to take in the angled barrel. It's almost like it isn't understanding its own world rotation.

avatar image AllTheGoodNamesWereTaken Bishmanrock · Feb 19, 2021 at 08:55 PM 1
Share

Is the empty weaponMuzzle still on the weapon? When you move your view up and down is the rotation of the weaponMuzzle changing in the editor?

Show more comments

2 Replies

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

Answer by JackhammerGaming · Feb 20, 2021 at 07:37 AM

try this @Bishmanrock

GameObject instantiated;

 void Start(){
 instantiated = Instantiate(projectilePrefab, weaponMuzzle.position, projectilePrefab.rotation); // this will instantiate the bullet as the same rotation set in your prefab
 
 //only use this if your position still does not work properly
 instantiated.transform.SetPositionAndRotation(weaponMuzzle.position , instantiated.transform.rotation); // if totation doesn't work here try quaternion.identity() function instead of instantiated.transform.rotation
 }
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 Bishmanrock · Feb 20, 2021 at 03:46 PM 0
Share

I stripped everything back and gave this a shot and it worked, thank you!

avatar image
0

Answer by VoidPhoenix96 · Feb 20, 2021 at 02:44 AM

Rotate the bullet spawn until it works.

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 Bishmanrock · Feb 20, 2021 at 02:32 PM 0
Share

Not sure what you mean? If I rotate the bullet spawn all it does is break the parts that are working, and makes the broken parts even more broken.

The bullet spawn is already facing the correct direction. The issue is related to the instantiated object not always picking up that direction. The bullet has also been confirmed to have been set to 0, 0, 0, so it's not a case of the direction of the prefab being off.

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

140 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

Related Questions

Why is it important to create an empty gameobject for my prefabs? 0 Answers

How to instantiate once when boolean changes 1 Answer

Instantiating enemies in spawn points not working on Android Build 0 Answers

Instantation and use of gameobject in same frame 2 Answers

Cloud objects not instantiating (Oh no!). 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