HELP WANTED - how to make bullets go in the direction of my gun

GUN SCRIPT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pistol : Weapon
{

private void Update()
{
if (Input.GetButtonDown(“Fire1”))
{
Shoot();
}
}

public override void Shoot()
{
//Calculate bullet direction

//Select random degrees angle between the range
float direction1 = Random.Range(-range, range);
//Transform that into radians
var rad1 = Mathf.Sin((direction1 * Mathf.PI) / 180);

//Sin(x) theorem
float B1 = Mathf.Sin(rad1);
float A1 = Mathf.Sqrt(1 - (B1 * B1));

//This is the direction the bullet will have
Vector2 lineDir1 = new Vector2(A1, B1);

//We create the bullet and apply all values
GameObject go = Instantiate(actualBullet, shot.transform.position, transform.rotation);
go.transform.localScale = transform.localScale;
Bullet b = go.GetComponent();
b.damage = damage;
b.lifeTime = bulletLife;
b.speed = bulletSpeed;
b.side = side;
b.lineDir = lineDir1;
b.bulletCollisions = bulletCollisions;
}
}

BULLET SCRIPT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
[HideInInspector]
public float damage;
[HideInInspector]
public float lifeTime;
[HideInInspector]
public float speed;
[HideInInspector]
public float side;
[HideInInspector]
public Vector2 lineDir;
[HideInInspector]
public string[ ] bulletCollisions;

public Rigidbody2D rb;
private float normalization;
private Vector3 normalizedOrientation;
private Collider2D col;
private SpriteRenderer sr;
private TrailRenderer tr;

private bool oneTime;

public float velocity = 10;
public float velX = 0f;
public float velY = 0f;

public virtual void Start()
{
oneTime = false;
rb = GetComponent();
col = GetComponent();
sr = GetComponent();
tr = GetComponent();

//Normalize the vector direction (It can be changed with lineDir = lineDir.normalized)
normalization = Mathf.Sqrt(Mathf.Pow(lineDir.x, 2) + Mathf.Pow(lineDir.y, 2));
normalizedOrientation = new Vector3(lineDir.x / normalization, lineDir.y / normalization);
}

void Update()
{
lifeTime -= Time.deltaTime;

if(lifeTime <= 0 && !oneTime)
{
FadeOut();
oneTime = true;
}

if (lifeTime <= -2)
{
Destroy(gameObject);
}

rb.velocity = new Vector2(velX * velocity, velY * velocity);
}

public virtual void FixedUpdate()
{
//Moves in the direction selected
//rb.velocity = new Vector2(side * speed * normalizedOrientation.x, normalizedOrientation.y * speed);
rb.velocity = transform.right * speed;
}

void OnTriggerEnter2D(Collider2D col)
{
//Collide with everything selected
foreach (string s in bulletCollisions)
{
if(col.gameObject.CompareTag(s))
{
FadeOut();
oneTime = true;
}
}

}

//Die function, but it keeps trail renderer alive till it disappears
public virtual void FadeOut()
{
col.enabled = false;
sr.enabled = false;
tr.emitting = false;
}
}

\\\\\\\\\\\\\\\\\\\

For some reason whenever i shoot the bullets always go right instead of whichever way the gun is rotated, does anyone have a solution??

Please post code using code-tags . Code pasted as plain-text is difficult to read. Also past your code including the original formatting/indents etc.

Welcome to Debugging 101! For your first class assignment, figure out why the bullets are not going the correct way. Here is how to start getting actual information you can reason about:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

You must find a way to get the information you need in order to reason about what the problem is.

1 Like