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 Faegan · Sep 09, 2013 at 03:47 PM · rigidbodyfpsspawnbulletgun

Rigidbody bullets won't spawn at gun barrel...

This is my first project in Unity and I am trying to create a simple FPS game. My problem is that no matter what I try, my rigidbody bullets won't spawn at the designated object but instead, somewhere bellow (relative to the gun). Here is the shooting script I created...

 public GameObject Spawn;
     public GameObject BulletPrefab;
     public GameObject Sound;
     public GameObject Gun;
     public ParticleSystem Emitter;
     public Light GunLight;
     public float EmissionTime;
     public float Recoil;
     public float BulletImpulse;
     public Camera cam1;
     public Camera cam2;
     float timer;
 
     void Start () {
     Emitter.enableEmission = false;
     GunLight.enabled = false;
     }
     
     void Update () {
     if (Input.GetButtonDown("Fire1") && Emitter.enableEmission == false && !Input.GetButton("Fire3")){
     timer = 0;
     GameObject newbullet = (GameObject)Instantiate(BulletPrefab,Spawn.transform.position,Spawn.transform.rotation); 
     Instantiate(Sound,transform.position,Camera.main.transform.rotation); 
     newbullet.rigidbody.AddForce(newbullet.rigidbody.transform.forward * BulletImpulse,ForceMode.Impulse);
     //newbullet.transform.position = new Vector3(Spawn.transform.position.x,Spawn.transform.position.y,Spawn.transform.position.z);
     Emitter.enableEmission = true;
     GunLight.enabled = true;
     }
     if (Emitter.enableEmission == true){
         timer += 80*Time.deltaTime;
         if (timer > EmissionTime){
             Emitter.enableEmission = false;
             GunLight.enabled = false;
         }
         }
         
     }

Sorry for the long post, thank you for your time.

Comment
Add comment · Show 1
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 EHogger · Sep 09, 2013 at 04:28 PM 0
Share

Your spawn transform position must not be where you think it is.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by citizen_rafiq · Sep 09, 2013 at 04:26 PM

// attach this script with your gun and put an empty game object as a child of gun which work as spawn point of bullet keep some distance between gun barrel and empty game object

public class AutoFire : MonoBehaviour {

 public float frequency = 10;
 public float coneAngele = 1.0f;
 public bool firing = false;
 public float hitSoundVolume = 5f;
 public GameObject muzzleFlash;
 public AudioClip soundHit;
 private float _lastFireTime = -1;
 private RaycastHit _hitInfo;
 private Quaternion _coneRandomRotation;
 private Vector3 _force;
 private GameObject _bullet;
 private Transform _spawnPoint;
 RaycastHit hitInfo;
 void Start () {
 if(muzzleFlash) muzzleFlash.active = false;
 _force = new Vector3(0,0,0);
 _bullet=Resources.Load("Prefabs/bullet", typeof(GameObject))as GameObject;
 if(!_bullet){
 Debug.LogError("keep bullet prefab in Resources/Prefabs/bullet");
 }
 foreach(Transform tr in transform){
 if(tr){
 _spawnPoint=tr;
 }
 }
 }
  
 // Update is called once per frame
 void Update () {
 if (firing) {
 if (Time.time > _lastFireTime + 1 / frequency) {
 //coneRandomRotation= Quaternion.Euler (Random.Range(-coneAngele, coneAngele), Random.Range(-coneAngele, coneAngele),0);
 _coneRandomRotation = Quaternion.Euler(0, 0, 0);
 GameObject go= Instantiate(_bullet,_spawnPoint.position,Quaternion.Euler(Vector3.zero)) as GameObject;
 SimpleBulletOrMissile bulletOrMissile = go.GetComponent<SimpleBulletOrMissile>();
 _lastFireTime = Time.time;
  
 Physics.Raycast (go.transform.position, go.transform.up,out _hitInfo,Mathf.Infinity,-5);
 if (_hitInfo.transform)
 {
 print(_hitInfo.transform.tag+"hitted");
 Health targetHealth = _hitInfo.transform.GetComponent<Health>();
 if (targetHealth)
 {
 //kill or health damage of target
 }
  
 if (_hitInfo.rigidbody)
 {
 //if want to apply force on enemy
 hitInfo.rigidbody.AddForceAtPosition(_force, hitInfo.point, ForceMode.Impulse);
 }
  
 AudioSource.PlayClipAtPoint(soundHit, _hitInfo.point, hitSoundVolume);
  
 bulletOrMissile.dist = _hitInfo.distance;
  
  
 }
 else {
  
 bulletOrMissile.dist = 50;
 }
 }
  
 }
 if(Input.GetMouseButton(0)){
 OnStartFire();
 }
 if(Input.GetMouseButtonUp(0)){
 OnStopFire();
 }
 }
  
 void OnStartFire() {
 if (Time.timeScale == 0)
 return;
  
 firing = true;
 if(muzzleFlash) muzzleFlash.active = true;
 if (audio) audio.Play();
 }
  
 void OnStopFire() {
 firing = false;
 if(muzzleFlash) muzzleFlash.active = false;
 if (audio) audio.Stop();
 }

//attach this script with bullet

public class SimpleBulletOrMissile : MonoBehaviour {

public float speed = 10; public float lifeTime = 0.5f; public float dist = 100;

 private float _spawnTime = 0.0f;
 private Transform _tr;
 // Use this for initialization
 void OnEnable () {
 _tr = transform;
 _spawnTime = Time.time;
 }
  
 // Update is called once per frame
 void Update () {
 _tr.position += _tr.forward * speed * Time.deltaTime;
 dist -= speed * Time.deltaTime;
 if (Time.deltaTime > _spawnTime + lifeTime || dist < 0) {
 // print("Destroy form bullet");
 Destroy(gameObject);
 }
 }
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 Owen-Reynolds · Sep 09, 2013 at 08:18 PM 0
Share

This is a single-frame raycast and firing a moving bullet?

avatar image
0

Answer by Owen-Reynolds · Sep 09, 2013 at 07:58 PM

The bullets have no way of knowing where to come out of unless you tell them. In your code, this line (cut down) tells the bullets where they should appear:

 GameObject newbullet = Instantiate( ... ,Spawn.transform.position, ...);
                                          ^^ bullet goes here ^^^^

Spawn.transform.position is just a fancy way to say transform.position, which is the gun's position -- where the little arrow-cluster is centered. That's probably somewhere at the base of the whole thing, not at the tip.

All you have to do is tell the bullet to spawn at the tip. As citizenrafiq mentions, the most obvious way is to mark the tip with an empty, as a child to the gun. Suppose the empty is named "tip." Add a line to look it up:

 Vector3 tip = transform.Find("tip"); // new line

 // your same spawn, but tell bullet to appear where "tip" is:
 GameObject newbullet = Instantiate( ... , tip , ...);
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

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

17 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

Related Questions

How to add Bullet Velocity 1 Answer

Dart/Bullet comes out pointing up 1 Answer

how to make bullets apply damage 1 Answer

Troubles With A Shoot Script 1 Answer

Issue with Bullet Movement shooting 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