- Home /
Shooting Cannons from a boat
Ok so I have 2 prefabs for the Cannon that my ship will shoot. They are called:
CannonLeft
CannonRight
The left cannon has this script:
using UnityEngine;
using System.Collections;
public class Move_Cannon_Left : MonoBehaviour
{
// Use this for initialization
public float speed = 1f;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
transform.Translate(speed, 0, 0);
}
}
The right one has this one:
using UnityEngine;
using System.Collections;
public class Move_Cannon_Right : MonoBehaviour {
// Use this for initialization
public float speed = 1f;
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(-speed, 0, 0);
}
}
It's basically the same thing except the way the x direction of the cannon moves is opposite to the other.
I have multiple bullet spawns which are empty game objects. They are called:
CannonSpawnsLeft - which is a parent to many of the actual empty game object called spawn(1,2,3 blah blah) and they are all tagged LeftBulletSpawn
CannonSpawnsRight- which is a parent to many of the actual empty game object called spawn(1,2,3 blah blah) and they are all tagged RightBulletSpawn
Here is the shoot script for the parent CannonSpawnsLeft and CannonSpawnsRight:
using UnityEngine;
using System.Collections;
public class ShootAllCannons : MonoBehaviour
{
#region BulletPrefabVariables
//Assign the bullet into the prefab. One for the left and one for the right.
public GameObject LeftCannonBullet;
public GameObject RightCannonBullet;
//apply a delay time for the bullets to prevent rapid fire
public float delayTime = 3f;
//a counter to check the current time before it is reset (see the functions below for the reset)
private float counter = 0;
//stored time variable to destroy the instantiated bullet after being spawned
public float destroyTime = 2.0f;
#endregion
void Start ()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Mouse0) && counter > delayTime) //left cannon
{
GameObject[] leftCannonSpawns = GameObject.FindGameObjectsWithTag("LeftCannonSpawn");
foreach (var _leftBulletSpawnPoint in leftCannonSpawns)
{
var bulletInstance = Instantiate(LeftCannonBullet, transform.position, transform.rotation);
Destroy(bulletInstance, destroyTime);
//audio.Play();
counter = 0;
} // end for each
} //end if
if (Input.GetKey(KeyCode.Mouse1) && counter > delayTime) //right cannon
{
GameObject[] rightCannonSpawns = GameObject.FindGameObjectsWithTag("RightCannonSpawn");
foreach (var _rightBulletSpawnPoint in rightCannonSpawns)
{
var bulletInstance = Instantiate(RightCannonBullet, transform.position, transform.rotation);
Destroy(bulletInstance, destroyTime);
//audio.Play();
counter = 0;
} // end for each
} //end if
counter += Time.deltaTime;
} //end update
}//end class
So now here is the problem. It technically works, all the bullets are being instantated, however they are all shooting out through one of the spawns. This means that if I shoot for the left cannons, all the bullets will be instantiated but it will look like one cannon was shooting all of these. the bullets will look like one bullet because they are all shot at the same time, but my hierarchy shows that there are multiple bullets equal to the amount of spawns.
The same thing happens to the right, but it doesn't spawn in the left spawn point. Again only one right spawnpoint is seen to be used. I know it has to be somewhere in their instantions. Their transform.translate and stuff is only applied to one of the spawns. How can I fix this?
Update: Ok so I figured it out, the transform.position was my fault. It should be directed towards the Instance of the spawn then .transform.position.
But now I have another problem. I have been looking around buoyancy scripts and I can't seem to find one that will benfit my boat. Everytime I use one, my boat will just sink to the bottom or fly super high in the air. Sometimes I get really close but the change in position from the boat's height in the water to the air is too high? (sorry if I sound dumb but do I need to adjust my damping?)
Answer by VildNinja · Aug 16, 2015 at 09:08 PM
You are referring to the ShootAllCannons transform NOT the transform of _leftBulletSpawnPoint. All your foreach loop is good for is counting the number of leftCannonSpawns. Remember to write _leftBulletSpawnPoint.transform instead of just transform.