Home > Software design >  Shooting in Unity3D; bullets not going straight or in the right direction
Shooting in Unity3D; bullets not going straight or in the right direction

Time:11-16

I'm making a top down shooter, but I'm having trouble getting the shooting exactly as I want it. Currently, I have a Player script that handles movement and shooting.

    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.transform, bulletSpawnPoint.transform.position, playerObj.transform.rotation);  
}

As of now, the bullets don't always come out in a straight line. When the player starts moving, the bullets seem to lag behind, and don't always come out of the bullet spawn point that I have set up at the tip of the weapon. So if the player is facing left or right, the bullets will come out behind the player to the left or right. If the player is facing forward, the bullet will always come out in front.

I've tried increasing bullet speed in the inspector, but it doesn't seem to fix the issue of the bullet not always coming straight out in the right direction. I was given the tip that using a forward vector might help it, but how would I change what I have to incorporate that?

CodePudding user response:

you could use:
playerObj.transform.forward
instead of:
playerObj.transform.rotation

CodePudding user response:

If I understood your problem, you are setting bullet rotation to be same as the player, instead you must set it to be same as Bulletspawner. Also instead of Bullet Transform you should instantiate Bullet as Gameobject

So your Instantiate should look like this.

   // Variables
    Gameobject bullet;
    Transform bulletSpawnPoint;
    
  //  Instantiate bullet at bulletSpawn
    Instantiate(bullet, bulletSpawnPoint.position, bulletSpawnPoint.rotation);  
  • Related