All my unity game up to this point have been non physics 2d games. I am wondering if someone can provide for me (or point me to) a simple game/code where the player decides the angle and the “power” that the projectile is to shoot.
Thanks,
Mark
All my unity game up to this point have been non physics 2d games. I am wondering if someone can provide for me (or point me to) a simple game/code where the player decides the angle and the “power” that the projectile is to shoot.
Thanks,
Mark
You can always grab stuff from the FPS Tutorial on the Unity Website, it tells you how to create a Rocket Launcher, which you can easily modify into a cannon (third person as well). All you would need to do is have the Rigidbody be affected by gravity and then have your angle and power be affected by some GUI sliders The Forward momentum can easily be changed via a variable from the Rocket script.
var projectile : Rigidbody;
//This is your object, it's a prefab Rigidbody
var initialSpeed = 10.0;
//You can set this from an outside variable
var reloadTime = 0.5;
//var ammoCount = 20;
private var lastShot = -10.0;
//Don't worry about those three things.
function Shoot ()
{
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot) // ammoCount > 0
{
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of
// the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
//ammoCount--;
}
//else
//Debug.Log("I can't Reload");
}
All you would need to do is have the slider give the “initial speed” or in your case “Power” to your object and then you can shoot it. You can experiment with how you want your cannon to angle itself too.
For example:
var PowerPercentage = SliderPosition;
//Actually I don't know how to figure out a slider and where it is (I've never done it). Look at the GUI resources at the Unity website, because it tells you how to do it there.
var maxPower = 100;
//This way you can tweak it in the inspector
private var power = maxPower * PowerPercentage;
//Where you figure out your power by your percentage from the slider. Angle is decided by another slider and by the angle of what shoots the object.
Bless Teriki for answering this. More patience than I’ll ever have ![]()
I’m used to this. Be a mod/admin of a forum where you have lots of kids asking the same questions; you tend to stay friendly regardless of seeing the same question asked a multitude of times. That and it’s not that big of a deal XP At least he knows now. (That and you share knowledge and knowledge gets shared back, :3)