So i am working on a top-down tank shoot em up and i have the controls working but not the cannon. I think im doing it right but its not fireing when i press my mouse 0 button have a look.
function update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var bullet = Instantiate(bulletPrefab, transform.Find("cannonShoot").transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(bullet.transform.forward * 2000);
}
}
"cannonShoot" is a empty game object in front of my tank's barrel
Also i need a way to rotate to a specific angle over a period of time... currently im just using snap to angles for movements as shown below.
private var z = 0.0;
private var x = 0.0;
private var y = 0.0;
function Update()
{
var leftButton = Input.GetKeyDown(KeyCode.A);
var upButton = Input.GetKeyDown(KeyCode.W);
var rightButton = Input.GetKeyDown(KeyCode.D);
var downButton = Input.GetKeyDown(KeyCode.S);
var angles = transform.eulerAngles;
//Basic Movements
if(leftButton)
{
var rotationLeft = Quaternion.Euler(x, -90, z);
transform.rotation = rotationLeft;
}
if(upButton)
{
var rotationUp = Quaternion.Euler(x, 0, z);
transform.rotation = rotationUp;
}
if(rightButton)
{
var rotationRight = Quaternion.Euler(x, 90, z);
transform.rotation = rotationRight;
}
if(downButton)
{
var rotationDown = Quaternion.Euler(x, -180, z);
transform.rotation = rotationDown;
}
//Two buttons at once
if(downButton && leftButton)
{
var rotationDownLeft = Quaternion.Euler(x, 45, z);
transform.rotation = rotationDownLeft;
}
}
Any help would be appreciated.