• 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 Jerkymushroom · Sep 18, 2020 at 08:50 PM · cameragameobjects

Bullets Emerge from Two Locations at Random

I am using a script to fire projectiles out of a weapon, and I noticed that they are emerging from two distinct locations. One location is the barrel of the gun, and the other depends on the players movement. While traveling right, the location is to the right of the barrel, and it is left of the barrel traveling left. When stationary, the bullets emerge from a point above the barrel.

The script controlling where bullets spawn is in BaseWeapon.cs. I have determined that the player camera position itself is moving at random. The below screenshot shows the camera position alternate between y = -0.5 and -1.3. There is no visual indication the camera is moving while playing the game (no shaking).

alt text

Below is the weapon as it appears in Unity.

alt text

And below is BaseWeapon.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BaseWeapon : MonoBehaviour
 {
     public bool singleFire;
     public float fireRate;
     public GameObject bulletPrefab;
     public Transform firePoint;
     public int magSize;
 
     protected int bulletsInMag;
 
     public float bulletSpeed, bulletHitForce, bulletLifeTime;
 
 
     public float reloadTime;
     public int weaponDamage;
 
     public WeaponManager weaponManager;
 
     protected float nextFireTime;
     protected bool canFire;
     protected bool firedSingleShot = false;
 
     // Start is called before the first frame update
     void Start()
     {
         bulletsInMag = magSize;
         UIManager.singleton.UpdateBulletCount(magSize);
     }
 
     // Update is called once per frame
     protected virtual void Update()
     {
         if (Input.GetKeyDown(KeyCode.R))
         {
             StartCoroutine(Reload());
         }
         else if (Input.GetAxisRaw("Fire1") > 0)
         {
             //Debug.Log("Fire1 state " + Input.GetAxisRaw("Fire1"));
             if (singleFire && firedSingleShot)
             {
                 firedSingleShot = false;
                 return;
             }
             else
             {
                 Fire();
 
                 if (singleFire)
                     firedSingleShot = true;
             }
         }
     }
 
     protected virtual void Fire()
     {
         if (canFire)
         {
             if (Time.time > nextFireTime)
             {
                 nextFireTime = Time.time + fireRate;
 
                 if (bulletsInMag > 0)
                 {
                     //Point fire point at the current center of Camera
                     Vector3 firePointPointerPosition = weaponManager.playerCamera.transform.position + weaponManager.playerCamera.transform.forward * 100;
                     RaycastHit hit;
                     if (Physics.Raycast(weaponManager.playerCamera.transform.position, weaponManager.playerCamera.transform.forward, out hit, 100))
                     {
                         firePointPointerPosition = hit.point;
                     }
                     firePoint.LookAt(firePointPointerPosition);
 
                     //Fire
                     Debug.Log(weaponManager.playerCamera.transform.position);
                     //Debug.Log(firePoint.position);
 
                     GameObject bulletObject = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
                     BaseBullet bullet = bulletObject.GetComponent<BaseBullet>();
 
                     //Set bullet damage according to weapon damage value
                     bullet.SetValues(weaponDamage, bulletSpeed, bulletHitForce, bulletLifeTime);
 
                     bulletsInMag--;
                 }
                 UIManager.singleton.UpdateBulletCount(bulletsInMag);
             }
         }
     }
 
     protected IEnumerator Reload()
     {
         canFire = false;
         UIManager.singleton.UpdateReloadIndicator();
 
         yield return new WaitForSeconds(reloadTime);
 
         bulletsInMag = magSize;
         canFire = true;
         UIManager.singleton.UpdateBulletCount(magSize);
     }
 
     //Called from SC_WeaponManager
     public void ActivateWeapon(bool activate)
     {
         StopAllCoroutines();
         canFire = true;
         gameObject.SetActive(activate);
     }
 }


1.png (23.9 kB)
2.png (235.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jerkymushroom · Sep 19, 2020 at 05:53 PM

I swapped in the weapon with another weapon, and when I swapped them back, both the original and new weapon worked as expected. :)

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 rh_galaxy · Sep 19, 2020 at 12:27 AM

The camera position should follow the the player since it is a child, but maybe it varies by one frames movement if the player is moving, and the update to the camera position hasn't happened yet that frame... I'm not sure.

Also I would do the single fire like this for it to work, since now you reset the firedSingleShot after one frame, and not after the player has released the trigger...

 if (Input.GetAxisRaw("Fire1") > 0.1)
 {
     if (!singleFire || !firedSingleShot)
     {
         Fire();
         if (singleFire)
             firedSingleShot = true;
     }
 } else {
     firedSingleShot = false;
 }

But I can't see how there could be two bullets Instantiated in one fire-event... but maybe I misunderstood this?

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 Jerkymushroom · Sep 19, 2020 at 01:53 PM 0
Share

Thanks for the tip about the single fire! In both screenshots of the console in Unity, the player was not moving during the firing of the weapon. Two bullets were not instantiated in one fire event, rather, the bullets came out of differing positions during firing (one bullet would appear from the barrel, but the next bullet would then appear above the barrel). I testing it further and found that swapping weapons (I have since implemented another weapon) fixes the problem, but the main weapon still behaves as described if it has not been swapped since the game began.

avatar image rh_galaxy Jerkymushroom · Sep 19, 2020 at 04:29 PM 0
Share

Found this: https://answers.unity.com/questions/1284417/gameobjecttransformposition-on-the-y-axis-keeps-ch.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Cameras Shakes Near Small Objects In-Game and In-Scene 0 Answers

Puttings GUI's between objects 1 Answer

How do I stop a moving object when the Camera sees it 1 Answer

why when my first person camera looks at to many gameobjects it becomes really sensitive? 1 Answer

How to make a camcorder show things that don't show normally 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