# Convert Object Rotation to X, Y values

EDIT: I’ve cleaned up the scripts a little since I posted, hopefully they’re a little easier to read. How do I convert the cannons rotation into the values I need for ballSpeedHorizontal and ballSpeedVerticle? Or am I going about this all wrong?

I have a cannon game object and I rotate the barrel of the cannon by holding a button. I’d like to shoot a cannon ball out of the cannon but the script I’ve created for the velocity of the ball needs x and y acceleration values. How can I convert the rotation amount of the cannon into the x and y acceleration values I need to get the ball to come out of the cannon at the correct angle? Here is the script I made for the cannon ball:

``````using UnityEngine;
using System.Collections;

public class CannonBall : MonoBehaviour {

public float ballSpeedHorizontal;
public float ballSpeedVerticle;

// Use this for initialization
void Start ()
{
//Give the cannon ball a velocity and direction when intsantiated
rigidbody2D.velocity = new Vector3(-ballSpeedHorizontal, ballSpeedVerticle, 0);

//Wait three seconds after instantiation and destroy the cannon ball
Destroy(GameObject.Find("CannonBall(Clone)"), 3);
}

// Update is called once per frame
void Update ()
{

}
}
``````

And here is the script I made for the cannon:

``````using UnityEngine;
using System.Collections;

public class CannonShoot : MonoBehaviour
{

public GameObject CannonBall;			// Cannon ball object
public bool isShooting;					// Should we shoot the ball
Animator shootAnimator;					// Cannon animato

// Use this for initialization
void Start ()
{
shootAnimator = GetComponent<Animator>();
}

//Spawn the cannon ball
void SpawnBall ()
{
Instantiate(CannonBall, BarrelTip.tipLocation, Quaternion.identity);
}

// Update is called once per frame
void Update ()
{
//Press Space to shoot a cannon ball
if (Input.GetKeyDown ("space"))
{
ShootBall();
}

//Press A to rotate counter clockwise
if (Input.GetKey ("a"))
{
//print ("Rotating Counter Clockwise!");
transform.Rotate(0, 0, Time.deltaTime*100);
}

//Press S to rotate clockwise
if (Input.GetKey ("s"))
{
//print ("Rotating Clockwise!");
transform.Rotate(0, 0, Time.deltaTime*-100);
}
}

//Trigger the ball shoot animation and print to screen
void ShootBall ()
{
print ("We shot a ball!");
shootAnimator.SetBool("isShooting", true);
SpawnBall();
}

//Reset the cannon to it's idle position
void ResetCannon ()
{
print ("We reset!");
shootAnimator.SetBool("isShooting", false);
}
}
``````

I’m pretty sure my question is incredibly convoluted and maybe I wasn’t asking the right question. The scenario is I have a rotating canon that can rotate up and down to aim. I want the cannonball to fire out of the cannon in the direction the cannon is pointing. The solution I found was to fire the cannonball along the right transform of the cannon. Here’s my solution if anybody ever stumbles onto this:

``````using UnityEngine;
using System.Collections;

public class CannonBall : MonoBehaviour
{

float ballSpeed = 25.5f;

// Use this for initialization
void Start ()
{
//Add velocity in the direction of the right transform of the cannon barrel
rigidbody2D.velocity = GameObject.Find("CannonBarrel").transform.right * (-ballSpeed);

//Wait three seconds after instantiation and destroy the cannon ball
Destroy(gameObject, 3);
}

// Update is called once per frame
void Update ()
{

}
}
``````