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 quintendc · Jul 18, 2019 at 04:58 PM · projectileweaponweapon system

shoot projectile from weapon

Hi,

i have a character class that can pickup a weapon object in the game and create an instance of that weapon so that it is attached to a point/socket of his body, the character/player can shoot a projectile so the character class will use the Weapon Class to instantiate a projectile Weapon.Shoot() again the weapon class uses a point/socket where to spawn the projectile.

the problem here is the weapon is in the correct position in the game when you move around but if you shoot the projectile it is not spawned where the Projectile socket is in the world.

instance of the weapon in the character script:

             GameObject weaponClone;
             weaponClone = Instantiate(Weapon.gameObject, WeaponSpawnSocket.transform.position, 
             Quaternion.identity) as GameObject;

             weaponClone.transform.parent = gameObject.transform;


instance of the projectile in the weapon script:

         projectileClone = Instantiate(ProjectileObject.gameObject, 
         gameObject.transform.position, ProjectileSpawnSocket.transform.rotation);
 
         Debug.Log(gameObject.transform);


thanks in advance,

Comment
Add comment · Show 5
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 myzzie · Jul 18, 2019 at 05:34 PM 0
Share
 Instantiate(ProjectileObject.gameObject, 
          ProjectileSpawnSocket.transform.position, ProjectileSpawnSocket.transform.rotation);
avatar image Vega4Life · Jul 18, 2019 at 05:36 PM 0
Share

$$anonymous$$aybe try using the ProjectileSpawnSocket.transform.position ins$$anonymous$$d of using the gameObject.transform.position. The pivot point on the gameObject my not be where you think it is.

avatar image quintendc Vega4Life · Jul 18, 2019 at 07:30 PM 0
Share

@Vega4Life, @myzzie it doesn't work, the thing is its spawning from the correct position if you look from the guns perspective relative space. so the gun is attached on my character for example my characters location is (2,0,1) my guns position should also be around that position but if is shoot and debug the position of that socket the global location is the same as in the local location for example (0,0,1) so the projectile spawns there and not at +- (2,0,1)

avatar image CCnockaert quintendc · Jul 18, 2019 at 08:19 PM 0
Share

Can you give us a screenshot of your hierarchy when the projectile is instantiated please ?

Show more comments

2 Replies

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

Answer by Cornelis-de-Jager · Jul 18, 2019 at 10:17 PM

Honestly @quintendc, I can't see anything wrong with that implementation. It might be how you setup the gameobjects and scripts, but there is no obivious mistakes in your code. For completion sake, I will attempt to write a small controller/Gun/Player script below that I believe can work. Wish me luck.

 // Attach to player object
 public class PlayerController: MonoBehaviour {

     PlayerInteraction pi;
     
     void Start () {
         pi = GetComponent<PlayerInteraction>();
     }
     ...
     
     // This is the prefab that is stored in the assets folder
     public GameObject Weapon;
     
     // Call this to equip the Gun
     public void EquipGun () {
         GameObject weaponClone = Instantiate(
                 Weapon, 
                 WeaponSpawnSocket.transform.position, 
                 Quaternion.identity,
                 transform // Instantiate it directly under its parent to avoid weird positioning effects.
             ) as GameObject;
             
         pi.gc = weaponClone.GetComponent<GunController>(); 
     }
 }


 // Attach to player
 public class PlayerInteraction : MonoBehaviour {
     
     public GunController gc;
     
     void LateUpdate() => HandleShooting();
     
     void HandleShooting () {
         
         // First check for null
         if (gc == null)
             return;
             
         // Check for input
         if (Input.GetKeyDown(KeyCode.MouseButton0)){
             gc.Shoot();
         }
     }
 }


 // Attach to Weapon
 public class GunController : MonoBehaviour {
     
     public GameObject projectTile;
     
     // We instantiate it at Vector3.Zero because position 0, 0, 0 relative to the gun is its center.
     // We don't have to worry about world position
     public void Shoot () => Instantiate(projectTile, new Vector3(0, 0, 1), Quaternion.Identity);
 }
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 quintendc · Jul 19, 2019 at 09:46 AM 0
Share

Ok after testing some things, i see that if i handle the shoot projectile method in the Weapon script everything works just fine.

like so:

// weapon script

     // Update is called once per frame
     void Update()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0))
         {
             SpawnProjectile();
         }
     }
 
 
     public void SpawnProjectile()
     {
         GameObject projectileClone;
         projectileClone = Instantiate(ProjectileObject.gameObject, ProjectileSpawnSocket.transform.position, ProjectileSpawnSocket.transform.rotation);
     }

but when i do this from my character script

 Weapon.SpawnProjectile();

it isn't working

avatar image quintendc · Jul 19, 2019 at 10:15 AM 0
Share

@Cornelis-de-Jager i see what i was doing wrong, i assigned a weapon prefab to the public field weapon and used that to instantiate the weapon. but a also used that as my reference to call the ShootProjectile() method. so i added and extra variable weaponPi and get the weapon script from the clone when i now do weaponPi.ShootProjectile its working perfectly thanks

so my character has now these variables : public AWeapon Weapon; public GameObject WeaponSpawnSocket; public AWeapon WeaponPi;

creating the instance of the weapon now looks like this :

             GameObject weaponClone;
             weaponClone = Instantiate(Weapon.gameObject, WeaponSpawnSocket.transform.position, Quaternion.identity) as GameObject;
             weaponClone.transform.parent = WeaponSpawnSocket.transform;
             WeaponPi = weaponClone.GetComponent<AWeapon>();



avatar image
0

Answer by JonPQ · Jul 18, 2019 at 07:35 PM

Try putting the shoot code in LateUpdate()... instead of in Update() by then... the weapon should have finished all it's movement positioning in the world, and the projectile should come out of the correct position.

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 quintendc · Jul 18, 2019 at 07:58 PM 0
Share

also that doesn't seems to work, you see that i set the transform parent of the gun to the character, but if i do something like this in the weapon script gameObject.transform.parent.name i get a null exception

as you can see in the screenshot the projectile should spawn at the beginning of the blue arrow the purple circle and follow the red arrow ins$$anonymous$$d it is spawned at the position where the purple circle is inside of the prefab and follows the forward axis of the world

alt text

preview.png (134.4 kB)
avatar image JonPQ · Jul 19, 2019 at 12:21 AM 0
Share

also make sure you are instantiating the bullet at the muzzle's World Position, and pointing along the muzzle's Forward/Z axis.
$$anonymous$$ake sure you are not using local positioning. instantiate at muzzleTransform.position. NOT muzzleTransform.localPosition also if you have aligned the muzzle transfor so the Z axis is pointing forwards... after you have created the bullet, you can set... bullet.transform.forward = muzzle.transform.forwards.

also your second line... should be spawning bullet at position of the sprocket. not GameObject.. possibley. unless that script is on the sprocket.

projectileClone = Instantiate(ProjectileObject.gameObject, ProjectileSpawnSocket.transform.position, ProjectileSpawnSocket.transform.rotation);

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

114 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

Related Questions

Click button to fire weapon. 3 Answers

Shooting multiple bullets 2 Answers

How do I stop my weapon from firing while reloading? 2 Answers

Shooting a bullet/ projectile PROPERLY 9 Answers

Can't set an int from seperate script 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