Hello!
I was following this tut:
but my bullets are flying up, instead straight from the gun. I tried multiple things that were suggested in the comments (vector2.up, detach firepoint etc.) but nothing works. Can someone tell me what is wrong with my code? I have:
-
the sprite is pointing up, like in the tud
-
I put the codes at the appropriate places
-
I also checked the tuts man gitHub, the code there wont work either.
If somebody could help me out here, I would be really happy! Thanks! 
I posted the same thing here, with pictures : Reddit - Dive into anything
im mostly focusing on 3D Games,
but as i can see in your code you got:
Vector2.up
which causes the problem most probably,
you would need to use the vector your looking at, in your case that would be Left/Right i think 
You want to use transform.right
instead of Vector2.up
in your Translate call.
1 Like
Is there any way to create a vector in the direction the mouse is pointing? because transform.right is good for looking right, but when you look left, the bullet flies to the right, through my player.
But I have a script, that I use to move the weapon around, is there a way to use it for the bullet?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
//Was brauchen wir? Die Waffe soll dem Mauszeiger folgen. Wie geht das mit Skripten? Wir müssen den WINKEL berechnen, in dem der Sprite dem Cursor folgt.
public float offset;
//Nun, da wir die Waffe drehen können, brauchen wir das eigentliche Schießen
public GameObject bullet;
//Die Bullet muss natürlich von irgendwoher kommen...
public Transform firePoint;
// cCountdown, bevor man wieder schießen kann.
private float timeBtwShots;
public float startTimeBtwShots;
private void Update()
{
//Zu erst müssen wir den allgemeinen Abstand zwischen Cursor und Sprite berechnen.
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; //direction = destination - origin
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //das berechnet den Abstand
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
if (timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(bullet, firePoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
//Die Bullets sind sehr einfach. Das Public GameObject ist unser Projektil, und wenn wir eine Taste drücken, wird dieses gespawnt
}
}
and this script is on the bullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullets : MonoBehaviour
{
public float speed; //Wie schnell ist die Kugel?
//Die Kugeln sollen natürlich kaputt gehen, wenn sie nicht auf Geister treffen
private void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}