My bullet keeps following its spawn point after being instantiate. Pls help
Im making a simple top down shooter, but
when I fire and rotate the player, my bullet follows my player rotation.
I think it looks cool but I want it to go in a straigh line. I have no idea how to fix this. Please help.
Here's my bullet script
public GameObject go;
private Rigidbody2D bRB;
public float bulletLifespan;
public float bulletLifespanReset;
public float bulletSpeed;
void Start()
{
bRB = GetComponent<Rigidbody2D>();
go = GameObject.Find("BulletSpawner");
}
// Update is called once per frame
void Update()
{
BulletMvm();
Despawn();
}
void BulletMvm()
{
Transform goTransform = go.GetComponent<Transform>();
transform.Translate(goTransform.transform.right * bulletSpeed * Time.deltaTime);
}
Here's where i spawn my bullet
public class BulletSpawner : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform spawnPoint;
//public NormalBullet s_NB;
public float timer;
public float fireRate;
// Update is called once per frame
void Update()
{
ActiveShoot();
}
public void ActiveShoot()
{
if (timer <= 0.0f)
{
Shoot();
timer = fireRate;
}
timer -= Time.deltaTime;
}
public void Shoot()
{
float x = spawnPoint.position.x;
float y = spawnPoint.position.y;
Vector2 sP = new Vector2(x, y);
ObjectPooler.Spawn(bulletPrefab, sP, Quaternion.identity);
}
}
Answer by Waterpolo7 · Dec 22, 2021 at 08:14 PM
Hi @NicoGm I think I know how to solve this. Currently the bullet will always go in the direction of the players 'right', even after the shot is launched. To not have this problem, add this line to your Start void:
Transform goTransform = go.GetComponent<Transform>();
Vector3 directionOfBullet = go.right;
This will make sure your direction doesn't change anymore. In the Update void you should now delete the:
Transform goTransform = go.GetComponent<Transform>();
and you change the transform.Translate to:
transform.Translate(directionOfBullet * bulletSpeed * Time.deltaTime);
*the whole bullet script so you can copy, the above part was for explaining:
public GameObject go;
private Rigidbody2D bRB;
public float bulletLifespan;
public float bulletLifespanReset;
public float bulletSpeed;
void Start()
{
bRB = GetComponent<Rigidbody2D>();
go = GameObject.Find("BulletSpawner");
//This is to make clear what direction he must go in: V
Transform goTransform = go.GetComponent<Transform>();
Vector3 directionOfBullet = go.right;
}
// Update is called once per frame
void Update()
{
BulletMvm();
Despawn();
}
void BulletMvm()
{
//This does the movement, but with the good direction.
transform.Translate(directionOfBullet * bulletSpeed * Time.deltaTime);
}
I hope this solves your problem. Notify me when it works :)
Your answer
Follow this Question
Related Questions
SImple, but how do I check a game object's position in an If statement? 1 Answer
how do i fix the compiler errors on unity 5? i have tried everything there is to try. 0 Answers
input.getaxis returning -1 by default 0 Answers
Y-axis movement stuck. Help me please :c,Not Moving by pressing Button :C. Help me please 0 Answers
Move gameobject when clicking on another gameobject 1 Answer