# Unity c# bullet randomly changes position

i have problem in unity im using this code to shoot bullets and it works but when im shooting bullets are moving left automatically or when i will aim it at right it shoots straight instead i'm using this code right now:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shootobj : MonoBehaviour {
public GameObject prefab;

void Start () {
prefab = Resources.Load ("projectile") as GameObject;

}

void Update () {
if (Input.GetMouseButtonDown (0)) {
GameObject projectile = Instantiate (prefab) as GameObject;
projectile.transform.position = transform.position + Camera.main.transform.forward * 2;
Rigidbody2D rb = projectile.GetComponent<Rigidbody2D> ();
rb.velocity = Camera.main.transform.forward * 40;
}
}
}


what could be causing such thing?

• Any reason you have Camera.main.transform.forward? Try changing to transform.forward. Apr 2 '18 at 13:41
• nope. unfortunately that doesnt helps. Apr 2 '18 at 14:00
• Then check your gameobjet where this shootobj script is attached, pretty sure the gameobject has y rotation of 90 or something similar and on shoot that 90 deg is getting added to your bullet rotation. Apr 2 '18 at 14:08
• nope i dont have any script attached, tried it with different objects but still same.. Apr 3 '18 at 6:26
• Do the bullets move left during air time or you’re shooting simply a straight line that’s left and not forward? Try using Debug.DrawRay and use the same direction to visualize and see that direction you’re passing is correct, perhaps the objects rotation is the problem Aug 16 '18 at 12:16

Unexpected Collisions

Effects like this can happen because the bullet is colliding with something very shortly after spawning - like a piece of your character or gun - or spawning inside another collider. When this happens, the physics engine tries to force the objects apart, which can deflect the bullet off-course.

Using physics layers or Physics2D.IgnoreCollision methods, you can ensure the bullet can't hit your character, which prevents most of these unexpected deflections.

Unexpected Directions

I notice you're using Rigidbody2D, which suggests you're making a 2D game. (This is one of those cases where including a screenshot in your question really helps).

In a 2D game, the physics happens in the xy plane. If your camera is looking at the xy plane, then Camera.main.transform.forward is pointing along the z axis - perpendicular to the plane of physics. The same goes for the transforms of any sprites or quads visible to the camera - their forward / z axis (the blue arrow in the editor's translation gizmo) points "into" the screen, not along it.

So when you tell the bullet to "go forward" in this sense, it's like if I told you not to go up/down/north/south/east/west, but "go ana" or "increase your spissitude" - those aren't directions you can go in your three-dimensional physics world!

This means your velocity assignment could end up being a no-op - adding zero velocity, so the only movement imparted on the bullet comes from collision resolution as described above - or if the forward vector is very slightly tilted relative to the xy plane you might get a small nudge in the direction of the tilt.

In 2D, you usually want to work with left & right vectors or up & down, instead of forward/back (which generally point into / out of the screen)

It might be worth setting the angular velocity to zero, before you run rb.velocity. Also i would explicitly state the rotation of each instantiation too. Good luck

void Update () {
if (Input.GetMouseButtonDown (0)) {
GameObject projectile = Instantiate (prefab) as GameObject;
projectile.transform.position = transform.position + Camera.main.transform.forward * 2;

projectile.transform.rotation = transform.rotation + Camera.main.transform.rotation;
Rigidbody2D rb = projectile.GetComponent<Rigidbody2D> ();

rB.angularVelocity = Vector3.zero;

rb.velocity = Camera.main.transform.forward * 40;
}
}