How to add shooting projectiles 2d platform

Hi Everyone,

First of all I’m very new to coding in general and I apologise if this is a stupid question but I have got as far as I can by myself and would really appreciate a little help, the most basic way you can explain it to me the better. #ExplainItLikeIm5.

I’ve been using Sebastian Lague’s 2D platformer controller tutorials to build my own platformer. Source code here. He bases the controller around using raycast2D to detect collisions. The problem is I’m not sure how to add the ability for the player to shoot projectiles and have them collide with an enemy. The way I understand it is the number of rays and the spacing between them gets calculated initially by the RaycastController.cs script by using the dimensions of the BoxCollider2D and sets up a layer mask. Which is then passed off into Controller2D.cs, and that does the heavy lifting for most things but importantly for collision it does hit detection and recalculates the rays based on the how the player has moved. The PlayerInput.cs script is there to give instructions for what buttons need to pressed to move and jump. The Player.cs script handles how fast the player moves, wall jumping, and all that jazz, and works alongside Controller2D.cs. So in theory I would create an enemy with the same setup for collisions as the player so I would need to RaycastController.cs, I would also need an enemy script to work with the Controller2D.cs to work out an enemy’s position, collisions and taking damage. Then I would need to have a Projectile script to hold damage and remove the projectile once something happens (it travels too far, hits something, hits an enemy). From there I figure I create a game object with a collider2D set to is (trigger) to act as the projectile?

So is my thinking on the right track, I tried this with a rigid body and it didn’t like that I was using addforce to shoot the projectile. Do I not add a rigid body, if so how do I “shoot” it, with transform.position? Sorry for the massive write up I just need to make sure I am at least somewhat understanding because otherwise the advice will go straight over my noob head.

Thank you!

Add rigid body 2d and also add box or circle collider on both the projectile and enemy and write the script to just detect the triggered button. Online search collider 2d scripts. Very easy. this will also help. remember colliders are used to detect collision not rigid body.

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {

// Reference to projectile prefab to shoot
public GameObject projectile;
public float power = 10.0f;

// Update is called once per frame
void Update () {
// Detect if fire button is pressed
if (Input.GetButtonDown(“Fire1”) || Input.GetButtonDown(“Jump”))
{
// if projectile is specified
if (projectile)
{
// Instantiante projectile at the camera + 1 meter forward with camera rotation
GameObject newProjectile = Instantiate(projectile, transform.position + transform.forward, transform.rotation) as GameObject;

// if the projectile does not have a rigidbody component, add one
if (!newProjectile.GetComponent())
{
newProjectile.AddComponent();
}
// Apply force to the newProjectile’s Rigidbody component if it has one
newProjectile.GetComponent().AddForce(transform.forward * power, ForceMode.VelocityChange);

}
}
}
}