- Home /
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,
Instantiate(ProjectileObject.gameObject,
ProjectileSpawnSocket.transform.position, ProjectileSpawnSocket.transform.rotation);
$$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.
@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)
Can you give us a screenshot of your hierarchy when the projectile is instantiated please ?
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);
}
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
@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>();
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.
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
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
Follow this Question
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