- Home /
Using Vector3 to make a bullet shoot in the direction the player is facing
I'm creating a top down shooter. In my update function, I control the camera and player movement. I also have a function that shoots. However, I'm having trouble getting the bullets to come straight out of the point I have set on the tip of the weapon, called bulletSpawnPoint. It'll shoot straight if the player is facing straight, but shoots backwards when facing left, right, or backwards.
void Update()
{
//Player face mouse
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDist = 0.0f;
if(playerPlane.Raycast(ray, out hitDist))
{
Vector3 targetPoint = ray.GetPoint(hitDist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
targetRotation.x = 0;
targetRotation.z = 0;
playerObj.transform.rotation = Quaternion.Slerp(playerObj.transform.rotation, targetRotation, 7f * Time.deltaTime);
}
//Player Movement
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * movementSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
//Shooting
if (Input.GetMouseButtonDown(0))
{
Shoot();
gunAudio.Play();
}
}
void Shoot()
{
Instantiate(bullet, bulletSpawnPoint.transform.position, playerObj.transform.rotation);
}
I was told that using a Vector3 when instantiating my bullet would fix this. But how would I do this? Everything I've tried gives me an error or makes it so the bullet doesn't come out of the tip of the weapon
Have you double checked to make sure the bullet isn't colliding the the barrel of the gun or part of the player? Also it's sometimes better to say saying like this
GameObject go = bulletPreab.Instantiate(bullet, bulletSpawnPoint.transform.position, Quaternion.Identity);
go.transform.forward = playerObj.transform.forward.
I tried this, I'm getting an error. So I tried this
GameObject go = bullet.Instantiate(bullet, bulletSpawnPoint.transform.position, Quaternion.identity);
go.transform.forward = playerObj.transform.forward;
bullet.Instantiate is giving me an error
I assume the bullet reference is not a gameobject? You can get a GO reference by doing
GameObject go = bullet.Instantiate(bullet, bulletSpawnPoint.transform.position, Quaternion.identity).gameObject;
Answer by LeFlop2001 · Nov 28, 2021 at 05:03 PM
The simplest way to copy the forward direction of an gameobject is using the transform.forward property.
void Shoot()
{
var bulletInstance = Instantiate(bullet);
bullet.transform.position = bulletSpawnPoint.transform.position;
bullet.transform.forward = player.transform.forward;
}
Im not sure whats causing your errors from the information given, so please provide more information in the future if you want a proper answer (what kind of error, the line in the code etc.)
By the way you dont need to manualy check the direction keys to get a movement direction. A better way would be to use the Input class GetAxis function.
void Move(){
var horDirection = Input.GetAxis("Horizontal") * Vector3.right;
var verDirection = Input.GetAxis("Vertical") * Vector3.forward;
var dir = horDirection + verDirection;
transform.Translate(dir * Time.deltaTime * movementSpeed);
}
Thank you for the reply, I appreciate the advice. I tried using what you suggested, and the issue still happens. When facing left or right, the bullets shoot backward. When facing backwards, the bullets shoot forwards.
can you please add how you are moving your projectile? The issue seems to be hidden there.
You should be doing something like transform.translate(transform.forward * Time.deltaTime * speed)
in your bullets update if you follow @LeFlop2001 s advice
Your answer
Follow this Question
Related Questions
crosshair help needed 2 Answers
Shooting causes character to shoot forward 2 Answers
Turret Range Script 2 Answers
How to change the speed of a projectile? 2 Answers