I am making a top-down shooter in Unity (following a Brackeys tutorial for the top-down shooting mechanic). When I press the left mouse button a bullet prefab spawns, as it should, however there is no force applied to the bullet. As you will be able to see in the code, I have applied force to the bullet, I am not sure why it won't work. Any ideas guys? Thank you in advance!
This is my shooting script:
#region Variables
// floats
private float bulletForce = 130f;
// bools
// rigidbodies
private Rigidbody2D rb = null;
// transforms
public Transform firePoint = null;
// game objects
public GameObject bulletPrefab = null;
private GameObject bullet = null;
#endregion
#region Awake
// called before the script is initialised
private void Awake()
{
}
#endregion
#region Update
// called ever frame - used for checking for input
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
#endregion
#region FixedUpdate
// called at a fixed time - every 0.2 seconds - used for physics
private void FixedUpdate()
{
}
#endregion
#region Method - Shoot
// method to spawn bullet prefab and shoot bullet
private void Shoot()
{
// spawn the bullet prefab at the position of fire point and rotate the prefab bullet to match the rotation of the player - name this newly created bullet by storing it in variable named bullet
bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
// store rigidbody on bullet game object in rb variable
rb = bullet.GetComponent<Rigidbody2D>();
// add force to the bullet - impulse makes the bullet fly straight away - also in this line of code i get the angle offset variable from the player controller script
rb.AddForce((firePoint.forward * gameObject.GetComponent<PlayerController>().angleOffset) * bulletForce, ForceMode2D.Impulse);
}
#endregion
}
This is my PlayerController script:
#region Variables
// floats
private float moveSpeed = 10f;
private float angle = 0f;
public float angleOffset = 270f;
// bools
// rigidbodies
private Rigidbody2D rb = null;
// vector2s
private Vector2 movement = Vector2.zero;
private Vector2 mousePos = Vector2.zero;
private Vector2 lookDirection = Vector2.zero;
// cameras
public Camera cam = null;
#endregion
#region Awake
// called before the script is initialised
private void Awake()
{
// store rigidbody component in rb variable
rb = GetComponent<Rigidbody2D>();
}
#endregion
#region Update
// called ever frame - used for checking for input
private void Update()
{
// listen for horizontal input from the player - a and d or left arrow key and right arrow key
movement.x = Input.GetAxis("Horizontal");
// listen for vertical input from the player - w and s or up arrow key and down arrow key
movement.y = Input.GetAxis("Vertical");
// find the mouse position, then convert the pixel position to world point and store that in a variable
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
#endregion
#region FixedUpdate
// called at a fixed time - every 0.2 seconds - used for physics
private void FixedUpdate()
{
// move the player to the desired position - fixed delta time happens every time fixed update is called
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
// find the direction the player needs to look by subtracting two vectors from each other
lookDirection = mousePos - rb.position;
// this finds the angle needed to calculate where the player should look then converts it to degrees
angle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg - angleOffset;
// rotate the player using the above maths
rb.rotation = angle;
}
#endregion
}
The bullet just spawns Infront of the sprite and doesn't move.
ForceMode2D.Impulse
which accomplishes an instantaneous change in momentum, similar to setting velocity. I agree setting velocity directly is simpler, but the current method is not dependent on the time interval of the physics tick. \$\endgroup\$