I am making a multiplayer pirate ship game and want to know how to make the cannons on left side of the ship fire when I press the left arrow key. But the thing is, I don’t know how to code the cannon to spawn an entity and make it shoot in the direction it is facing… How would I do this?
You do it like this — in particular, see the second example, right under “Instantiate is most commonly used to instantiate projectiles…”
Thanks
How do you create a timer that sets the reload time of the cannon so they can’t shoot until the cannon finishes reloading
Simply add a “nextFireTime” float property to your script. When you are about to fire, if Time.time < nextFireTime, then don’t do it (just return or whatever). Otherwise, fire your shot, and set nextFireTime = Time.time + 3 (or whatever reload time you want).
Thank you.
I would suggest using a pooling system, so it is good for performance
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
Also, the cannonball has to appear a certain distance from the the cannon, how would I do that?
Hello,
Give an offset to the cannonball based on the cannon position.
Do not understand… I only have limmited experience in coding
I typically cheat and place an empty transform at the projectile spawn point.
public Vector3 muzzleOffset = new Vector3(0, 0.34f, 1.3f);
//defines the muzzle position at 0 to the right, 0.34 upwards, and 1.3 forward of the cannon pivot (= transform.position).
public Vector3 muzzlePos;
void Start() {
muzzlePos = transform.position + transform.right * muzzleOffset.x + transform.up * muzzleOffset.y + transform.forward * muzzleTransform.z;
}
The last thing I need to ask on this topic is how do you add a delay to the cannon fire? I don’t wan’t the cannons to fire all at the same time, I would like them to fire in a specific order. Like the cannon closest to the front of the ship fires first, the the second closest, then so on and so forth.
You can use a CoRutione for that. It is like a normal function, but it can run for any length of time, meaning that you can have one or more pauses in between code lines.
public Cannon cannon_array; //fill this array with your Cannon scripts that sit on gameObjects
public bool firing = false;
public float timeBetweenCannons = 0.5f;
public float timeAfterSalvo = 2f;
void Update() {
if(firing == false) {
StartCoroutine("FireCannons");
}
}
private IEnumerator FireCannons() {
firing = true;
foreach(Cannon cannon in cannon_array) {
cannon.Fire();//call the function that fires your cannon
yield return new WaitForSeconds(timeBetweenCannons);
}
yield return new WaitForSeconds(timeAfterSalvo);
firing = false;
}
Thank you
you might want to have a look at some of the tutorials and live training in the learn section. The basics of “shooting stuff” is covered quite extensively.
Unity Learn (space and survival shooter looks like good candidates for what you’re trying)
http://unity3d.com/learn/live-training (you’ll have to pick through, but there are ones on pooling as suggested above and shooters in general, new stuff most tuesdays )