Object going different direction than forward.

Hey guys.
I want it to shoot straight out infront, yet it is shooting almost randomly in a weird position or even moving the bullet at all… (very strange)

My code:

public Rigidbody bulletObject;
	public Transform bulletSpawnPoint;
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown(0)) {
			Rigidbody fireBullet = Instantiate(bulletObject, bulletSpawnPoint.position, transform.rotation) as Rigidbody;
			fireBullet.AddForce(Vector3.forward * 300);
		}
	}

No errors, just not shooting properly. Can someone help me please?

BUMP, I need this helped fast as possible please guys :cry:

If your bullets have colliders, there’s a chance that the bullets may be colliding with each other before they can get very far. As long as the mouse button is held down, you’re spawning a physics rigidbody every single frame.

Depending on your game style, a more traditional FPS method would be to use a raycast to simulate the bullet instead of actually creating a bullet object.

AddForce uses the global coordinate system, so your bullets will always fire along the z-axis, regardless of which way your gun is facing. To make them fly in the direction they’re pointing use fireBullet.transform.forward * 300.

Alright, so now I’m getting some weird errors I don’t understand how to fix.

My code:

void Update () {

		RaycastHit hit;
		Ray ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));

		if (Input.GetMouseButtonDown(0)) {
			if (Physics.Raycast(ray, hit, bulletDistance)) {
				hit.transform.SendMessage("ApplyDamage", bulletDamage, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

errors:

Try this:

void Update () {

		RaycastHit hit;
		Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0f));

		if (Input.GetMouseButtonDown(0)) {
			if (Physics.Raycast(ray, hit, bulletDistance)) {
				hit.transform.SendMessage("ApplyDamage", bulletDamage, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

Sweet that did help a little!
But still a little bit more:/

Okay, try this:

void Update () 
{
	RaycastHit hit;
	Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0f));

	if (Input.GetMouseButtonDown(0)) 
	{
		if (Physics.Raycast(ray, out hit, bulletDistance)) 
		{
			hit.transform.SendMessage("ApplyDamage", bulletDamage, SendMessageOptions.DontRequireReceiver);
		}
	}
}

Thank you so much man!
You helped me:)

Happy to help :smile: