At the moment I am working on a pirate game but there is a bug I cannot seem to fix. The C# script below is powering the cannons. when I fire them on everyone of them fire forward, even though I have rotated the bullet spawns to face sideways from the ship. Please tell me if it is something wrong with my code and what it is, I have a feeling that it might be the “Quaternion.Identity” is making it point in the direction the ship is rather than the ball spawns.
Thankyou for your help.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Cannons : MonoBehaviour {
public Transform[ ] cannons = new Transform[4];
public int activeCannon = 0;
public Transform cannonBall;
public Transform gunSmoke;
public float bulletSpeed = 3000.0f;
void Start () {
}
// Update is called once per frame
void Update () {
float wheel = Input.GetAxis (“Mouse ScrollWheel”);
if (wheel > 0)
{
activeCannon++;
if (activeCannon > cannons.Length-1) activeCannon = 0;
}
else if (wheel < 0)
{
activeCannon–;
if (activeCannon < 0) activeCannon = cannons.Length-1;
}
//fire
if(Input.GetButtonDown (“Fire1”))
{
Transform bullet = (Transform)Instantiate(cannonBall, cannons[activeCannon].position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
Transform smoke = (Transform)Instantiate(gunSmoke, cannons[activeCannon].position, Quaternion.identity);
}
}
}