Question by
Madkrumper9 · Sep 30, 2020 at 09:05 PM ·
c#verticalcannoncannonball
Cannons wont move on the vertical axis
My cannon script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cannon : MonoBehaviour
{
public GameObject barrel;
private int LookVertical;
private int LookHorizontal;
public GameObject cannonBall;
public Transform spawn;
public int thrust;
public float degrees;
public Rigidbody CannonBallRb;
void Start()
{
}
// Update is called once per frame
void Update()
{
degrees = barrel.transform.rotation.x;
if (CheckingForInteractions.HighlightCannon == true)
{
barrel.GetComponent<Renderer>().material.color = Color.red;
}
else if (CheckingForInteractions.HighlightCannon == false)
{
barrel.GetComponent<Renderer>().material.color = Color.blue;
}
if (CheckingForInteractions.UsingCannons == true)
{
Debug.Log("cannons");
barrel.transform.Rotate(LookHorizontal, 0, 0);
if (Input.GetKey(KeyCode.UpArrow) && barrel.transform.rotation.x < 0.95)
{
LookHorizontal = 20;
}
else if (Input.GetKey(KeyCode.DownArrow) && barrel.transform.rotation.x > 0.75)
{
LookHorizontal = -20;
}
else
{
LookHorizontal = 0;
}
if (Input.GetKeyDown(KeyCode.Space))
{
FireCannon();
}
}
void FireCannon()
{
spawn.rotation = barrel.transform.rotation;
GameObject clone = Instantiate(cannonBall, spawn.position, spawn.rotation) as GameObject;
CannonBallRb = clone.GetComponent<Rigidbody>();
CannonBallRb.AddForce(-transform.forward * thrust * 500);
}
Everything else works except for firing up and down.
Comment