I have a cannon i want to control (control the angle and shoot it) but not when i start the scene. Only after right clicking on it. I tried it like this:
public class Shoot : MonoBehaviour
{
private float angle;
public GameObject Cannon;
public Text angleText;
// Update is called once per frame
public Rigidbody projectile;
public float horizontalSpeed = 2.0F;
public void Update()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, 0))
{
ControlCannon();
}
}
}
public void ControlCannon()
{
float h = horizontalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(h, 0, 0);
Text();
if (Input.GetButtonDown("Fire1"))
{
Rigidbody clone;
clone = Instantiate(projectile, GameObject.Find("Firepoint").transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.up * 10);
}
}
void Text()
{
angle = Cannon.transform.rotation.eulerAngles.x - 270;
angleText.text = "Angle: " + angle.ToString();
}
}
But it doesn’t do anything when i click on the cannon. Is there a different way or am i doing something wrong here?