0
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ Any reason you have Camera.main.transform.forward? Try changing to transform.forward. \$\endgroup\$
    – saurabhs
    Apr 2 '18 at 13:41
  • \$\begingroup\$ nope. unfortunately that doesnt helps. \$\endgroup\$
    – Nikka
    Apr 2 '18 at 14:00
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – saurabhs
    Apr 2 '18 at 14:08
  • \$\begingroup\$ nope i dont have any script attached, tried it with different objects but still same.. \$\endgroup\$
    – Nikka
    Apr 3 '18 at 6:26
  • \$\begingroup\$ 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 \$\endgroup\$ Aug 16 '18 at 12:16
1
\$\begingroup\$

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)

\$\endgroup\$
0
\$\begingroup\$

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;
        }
    }
\$\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.