0
\$\begingroup\$

I have recently made the simplest gun and bullet script that I could and it works amazingly, except for one problem that I've found. When I attach it to a game object with a rigid body the force of the bullets move the object back with quit a bit of force.

But what I can't seem to figure out is why the bullets are imparting force on the object at all. The bullets do not have a rigid body component. The way my guns work is with two scripts, one which acts as a simple spawning point when a key down event is given and what ever object you give it to spawn.

The second script is applied to the bullet itself which causes the bullet to have motion through transform.Translate with a speed variable. The best example I can give of this is when I attached two guns to the default unity jet and when I fired, it had enough force to move the jet back against its thrust speed.

I really would appreciate any help with this so I can move on with my project. I will provide the code bellow for the two scripts.

Thank you

public class Shoot : MonoBehaviour {
  public GameObject bullet;

  void Update () {
    if (Input.GetKey (KeyCode.Mouse0))
      Instantiate(bullet, transform.position, transform.rotation);
  }
}

public class MoveBullet : MonoBehaviour {

  public float speed = 1f;

  void Update () {
    transform.Translate (0, 0, speed);
  }
}
\$\endgroup\$
  • \$\begingroup\$ It sounds like its simulating a real effect: recoil. Try giving your bullets a much lower mass. Then the net recoil will be much lower. \$\endgroup\$ – Octopus Dec 2 '16 at 16:54
  • \$\begingroup\$ What happens when you remove the bullet script from the bullet and just instantiate it? Does it still push the object back? If so, then the problem is with collision. Check whether the bullet has any colliders (mesh or box collider etc.) even if it doesn't have a rigidbody. If it doesn't have any colliders, try instantiating the bullet in front of the gun instead of where the gun is (this is a bit hard to do if you are facing a random direction, so spawn it a coordinate or two away from the gun instead at first). \$\endgroup\$ – John Hamilton Dec 2 '16 at 17:30
1
\$\begingroup\$

your problem happened when merging two Boxcollider: When you attach rigid body to game Object the force of the bullets move the object back with quit a bit of force.

Adding a Rigidbody component to an object will put its motion under the control of Unity's physics engine. Even without adding any code, a Rigidbody object will be pulled downward by gravity and will react to collisions with incoming objects if the right Collider component is also present.

enter image description here

you should Instantiate bullet in front of gun but you Instantiate it Inside the gun so try this: make a empty game object place it child of gun(in front of gun)

enter image description here

using UnityEngine;
using System.Collections;

public class gun : MonoBehaviour {
    public GameObject bullet;
    public Transform ShotPos;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
            Instantiate(bullet, ShotPos.position, transform.rotation);
    }
}

other way is making a vector as shotPos:

using UnityEngine;
using System.Collections;

public class gun : MonoBehaviour {
    public GameObject bullet;
    public Vector3 ShotPos;

    void Awake()
    {
        ShotPos = transform.position + new Vector3(0, 0, 1);// transform.position + transform.forward
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        Instantiate(bullet, ShotPos, transform.rotation);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.