0
\$\begingroup\$

I'm making a top down shooter, and the movement is working fine.

However, I'm having trouble getting my Fire function to work correctly. When I run the game, it kind of fires in the right direction, but the more I move, the more off the shooting becomes. Anyone have any idea why?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerMovement : MonoBehaviour
{
    private float _horizontalInput = 0;
    private float _verticalInput = 0;

    public int movementSpeed = 2;
    public int rotationSpeed = 2;

    Rigidbody2D rb2d;

    float coolDown = 0;

    public Rigidbody2D bulletPrefab;
    public float attackSpeed = 0.5f;
    public float bulletSpeed = 500;
    public float yValue = 1f; // Used to make it look like it's shot from the gun itself (offset)
    public float xValue = 1f; // Same as above

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {

        MovePlayer();
        RotatePlayer();
    }

    void Update()
    {
        GetPlayerInput();

        // Determines if the player can fire
        if (Time.time >= coolDown)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Fire();
            }
        }
    }

    private void GetPlayerInput()
    {
        _horizontalInput = Input.GetAxisRaw("Horizontal");
        _verticalInput = Input.GetAxisRaw("Vertical");
        

    }

    public void MovePlayer()
    {
        rb2d.velocity = transform.right * Mathf.Clamp01(_verticalInput) * movementSpeed;
    }

    private void RotatePlayer()
    {
        float rotation = -_horizontalInput * rotationSpeed;
        transform.Rotate(Vector3.forward * rotation);
    }

    private void Fire()
    {
        Rigidbody2D bPrefab = Instantiate(bulletPrefab, new Vector3(transform.position.x + xValue, transform.position.y + yValue, transform.position.z), transform.rotation) as Rigidbody2D;

        bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.position * bulletSpeed);
        coolDown = Time.time + attackSpeed;
    }
}
\$\endgroup\$
0
1
\$\begingroup\$

Two mistakes on this line:

bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.position * bulletSpeed);
  1. You're passing a position where it looks like you want to pass a direction.

    Try transform.right to fire along the object's x (red) axis, or transform.up to fire along the object's y (green) axis. If you need a different direction, edit your question to include a diagram.

  2. You're passing a velocity to a function that expects a force.

    Try Vector2 velocity = transform.right * bulletSpeed; then either...

    bullet.GetComponent<Rigidbody2D>().velocity = velocity;

    or

    var body = bullet.GetComponent<Rigidbody2D>();
    body.AddForce(body.mass * velocity, ForceMode2D.Impulse);
    

Also, don't name your instance variable bPrefab with "prefab" in the name - it's not a prefab, it's an instance of the prefab, which is a different object. Misleading variable names eventually come back to bite us in confusion down the line, so keep them well-matched to their actual meaning.

\$\endgroup\$
3
  • \$\begingroup\$ My character is a tank the can rotate 360 degrees. I'm trying to make the bullet spawn whichever way the character is facing and fire in that direction if that helps at all \$\endgroup\$ – ang1019 Dec 16 '20 at 18:48
  • \$\begingroup\$ This depends on how you've set up your character. If the barrel points along the local x+ (red) axis, then you want transform.right. If it points along the local y+ (green) axis, then you want transform.up. If you need something else, I'll repeat my request to add a diagram to your question illustrating what you need. \$\endgroup\$ – DMGregory Dec 16 '20 at 18:52
  • \$\begingroup\$ transform.right worked! Thank you so much \$\endgroup\$ – ang1019 Dec 16 '20 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.