Slider to determine angle and adding force to projectile?

Hi,

I want to make a visible slider on screen that turns a canons tip from 0-90 degrees and depending on the angle it’s set to, add that as the direction in which the projectile is shot with RigidBody.AddForce. Does anybody know how to achieve this in C#?

It’s easy:

  • put slider on screen
  • set the min and max values 0f and 90f
  • attach a script to the slider or to the cannon
  • implement the OnValueChanged event
  • inside the event set the proper local rotation value of the cannon tip game object by the value you got from the slider
  • then when you fire the cannon just read the global rotation of the object and add impulse force and multiply it with some thrust value you want after you spawned the projectile at the tip of the barrel (or reused it from some object pool or something)

Some code would be nice for this : “inside the event set the proper local rotation value of the cannon tip game object by the value you got from the slider”

It sounds easy that’s for sure

You can find countless tutorials on UnityEvents, Slider value changes, rotation or even combined (I think this one is doing exactly what you’re looking for):

https://www.youtube.com/watch?v=bxheIjLAXLE

1 Like

Thank you for the link I’ll check it out. All the best