- Home /
When player shoot a butllet, the bullet does not go straight at what the player is facing.
Hello, I am trying to shoot without using mouse position. My player rotate to the right o the left. The player is the middle center of the screen in a fixed point. in the middle of the screen. When the player shoot the bullet goes up. If I change the script, the bullet goes up and right or left but never goes at what the player is facing.
here is the code I have.
PlayerScript.c# [SerializeField] GameObject bulletPrefab; [SerializeField] GameObject bulletPosition;
private void Rotate(float rotation) // rotate to the left or right { transform.rotation = Quaternion.Euler(Vector3.forward * rotation); }
void Shoot() { GameObject bullet = Instantiate(bulletPrefab, bulletPosition.transform.position, bulletPosition.transform.rotation) as GameObject; bullet.GetComponent().velocity = new Vector2(0, 1) * 5f; }
I had try : -AddRelativeForce -AddForce
But nothing works. The most near script that works for me is Vector2 direction = new Vector2(bulletPosition.transform.position.x, bulletPosition.transform.position.y); direction.x = -1; Rigidbody2D rb = bullet.GetComponent(); rb.velocity = (direction -1) * speed;
but still does not work properly.
Thanks.
Answer by wewewu · Oct 26, 2020 at 06:35 PM
GameObject bullet = Instantiate(bullet, transform.position, transform.rotation) bullet.GetComponent().velocity = bullet.transform.forward * speed;
this script should be apply to the camera
Answer by reddaj · Oct 26, 2020 at 09:43 PM
Thank you, I solved this issue. I have to add a bullet script to the bullet itself. That works super fine for me.
Your answer
Follow this Question
Related Questions
Bullet Collision Simulation? 2 Answers
Object follow array of points 0 Answers
healthbar with scale 0 Answers
2D Bullet Hell Game - Bullet Patterns 2 Answers
Bullets Based on Orientation 1 Answer