This question was
closed Oct 02, 2020 at 07:42 PM by
Madkrumper9.
Question by
Madkrumper9 · Sep 30, 2020 at 08:07 PM ·
c#verticalcannoncannonball
cannon wont fire on vertical axis!
I have a cannon that fires with this 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);
}
the problem is that my cannon doesn't fire at a vertical angle. The horizontal angle, spawn point, and power all work fine i just can't get it to fire upwards at all.
Comment
Follow this Question
Related Questions
Cannons wont move on the vertical axis 0 Answers
Time.deltaTime doesn't help on different devices! 0 Answers
Automatic cannon aiming 2 Answers
Set vertical text GUI layout 0 Answers
Hi, got a problem with my vertical axis. 0 Answers