I am having a severe problem understanding how to create a 2D cannon

Hey there guys and gals. I am trying to create a clone of Angry Birds for my own learning purposes and rather than using a slingshot I’m using a cannon. My setup, which will be pictured below, is 2 sliders and a button to launch my projectiles.

Right now, on update (when I have my button selected), I can see my power & rotation of the cannon on update based on my slider values and I essentially want to be able to give those values to the robot inside the cannon and fire it when I press the button.

I can’t seem to get anything to work. I’ve tried various attempts at instantiating an object but I must really be bad at this (note that this is one of my first “real” projects outside of tutorials).

If anyone can help point me in the right direction for what I’m missing I would greatly appreciate it because I’ve been pounding my head against the wall all week on this one and nothing seems to work.

Thank you. Here are the images and please let me know if there’s anything I can do to help you help me.

The rotation slider inspector

The power slider inspector

The launch button inspector

The launcher inspector (note that the launcher is made up of 3 parts, 2 of which are parent-child related. The glass rotation controls the rotation of the cannon as well as the (expected) trajectory of the robot)

The robot’s inspector

Here are the scripts you see in the screenshot (the robot’s script is currently empty).

First the launcher

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Launcher : MonoBehaviour
{
	public float rotationMin = 0.0f;
	public float rotationMax = 45.0f;
	public Slider rotationSlider;
	public Slider powerSlider;
	public GameObject botToLaunch;
	public float launchPower;
	[HideInInspector] public float timeSinceLaunched;

	[SerializeField] private float currentRoation = 25.0f;

	// Use this for initialization
	void Start() 
	{
		rotationSlider = GameObject.Find("RotationSlider").GetComponent<Slider>();
		powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
	}
	
	// Update is called once per frame
	void Update() 
	{
		transform.localEulerAngles = new Vector3(0.0f, 0.0f, rotationSlider.value);
	}

}

Then we have the launch power script attached to the button

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class LaunchPower : MonoBehaviour
{
	public float power;
	public float rotation;
	public Rigidbody2D botToLaunch;

	[SerializeField] private Launcher launcher;

	// Use this for initialization
	void Start() 
	{
		// Find the launcher
		launcher = GameObject.FindObjectOfType(typeof(Launcher)) as Launcher;

		// Get the values
		power = launcher.powerSlider.value;
		rotation = launcher.rotationSlider.value;
	}

	// FixedUpdate is called every physics step
	void FixedUpdate()
	{
		power = launcher.powerSlider.value;
		rotation = launcher.rotationSlider.value;
	}

	public void Launch()
	{
		botToLaunch.AddForce(new Vector2(power, rotation));
	}
}

Ignoring instantiation - what am I missing in order to even be able to fire a single bot from this thing?

Thanks so much in advance and I will closely monitor this post to answer any questions you have that may help you help me!

Hi,

Have you tried setting “isKinematic” to false on your robot’s Rigidbody2D component?

Also, you can simplify your LaunchPower.cs code :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
 
 public class LaunchPower : MonoBehaviour
 {
     public Rigidbody2D botToLaunch;
 
     [SerializeField] private Launcher launcher;
 
     void Start() 
     {
         // Find the launcher
         launcher = GameObject.FindObjectOfType(typeof(Launcher)) as Launcher;
     }
 
     public void Launch()
     {
       botToLaunch.AddForce(new Vector2(launcher.powerSlider.value, launcher.rotationSlider.value));
     }
 }