Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by imnickb · Jul 31, 2014 at 01:32 AM · rotationangleprojectiley axisx axis

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);
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 31, 2014 at 01:52 AM 1
Share

This appears to be 2D. With 2D your cannon with when it has rotation (0,0,0), the front of the barrel of the cannon will point either right or up. So you can get your x and y from either transform.right, or transform.up of the cannon game object. Note in Unity, usually the x and y for this kind of thing is not separated out. Ins$$anonymous$$d it is handled by using adding vectors.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by imnickb · Jul 31, 2014 at 11:21 PM

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 ()
     {    
 
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find out x at y 0 Answers

Place object on radius pointing to a other object 1 Answer

Determining the angle of a projectile collision. 2 Answers

How would I go about finding the rotation corrresponding to the force added to the y and z axis of a object? 1 Answer

Bone Rotation on X (or any) axis Issues 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges