bullets acting weird when spawn

When I spawn a bullet in-game, it sometimes moves to the left or right before going forward. Also, if I spawn one bullet going in one direction and another in a different direction, it seems to yank the other bullet in the direction it’s going for a short distance. Here is a link to a video of this happening:
https://drive.google.com/open?id=0B_HzgsC9lP3nS2RkUC1xRkU1Tmc
Here is my bullet spawning script:
using UnityEngine;
using System.Collections;

public class BulletController : MonoBehaviour {

    public GameObject BulletSpawn;
    public GameObject Bullet;
    GameObject BulletClone;
    public float BulletSpeed;
    void Update()
    {
        if (Input.GetKeyDown("space") == true)
        {
            BulletClone = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
        }
        Rigidbody rBody = GetComponent<Rigidbody>();
        rBody.velocity = BulletClone.transform.forward * BulletSpeed;
    }
}

Why are my bullets acting weird?

Perhaps they are collising with each other?

They might be colliding with the gun

As someone already mentioned they might be colliding with the gun. Can you set their trigger off?

just saw your link to the video... As everyone said, it does look like it is hitting something. OR You or it's adding another force somewhere along the process.

Hi Roman, thanks for your advice. Getting rid of the return; and using the check you proposed above did not solve the problem - this was actually how I tried it before. Since I do not use any rigidbodies, I have no way of using continous collision detection, or does the CharacterController support this as well?

2 Answers

2
  1. All physics stuff should be done in FixedUpdate(),

  2. It is not good to directly change the velocity of a “Rigidbody”, you should be using forces to get desired velocity,

  3. Try this instead:

    Instead of Update() use fixed.

    void FixedUpdate()
    {

                 if (Input.GetKeyDown("space") == true)
                 {
                     BulletClone = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
                     Rigidbody rBody = GetComponent<Rigidbody>();
                     rBody.AddRelativeForce(BulletClone.transform.forward * BulletSpeed,ForceMode.Impulse);
                     //rBody.AddRelativeForce(BulletClone.transform.forward * BulletSpeed, ForceMode.VelocityChange);  //<---Note ForceMode.VelocityChange, try this mode also, it ignores the mass
             }
     
     
         }
    

The bullets are still not going straight.

here is a script that should make the bullets fire right. but bring your spawn point out a little more.

using UnityEngine;
using System.Collections;

public class shotSpeed : MonoBehaviour {
    public float speed;
    public Vector3 target;
    public Transform target1;
    // Use this for initialization
    void Start ()
    {
        target = target1.transform.position;
    }
	
	// Update is called once per frame
	void Update ()
    {
        
        var delta = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target, delta); // move towards        //target

        if(transform.position == target)
        {
            Destroy(gameObject);
        }
    }
}

Absolutely, as mentioned above there is another collider with isTrigger=True somewhere down the hierarchy but that one has been disabled for testing. This is also the only instance of a model using this particular script in the entire scene, and project for that matter.