Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by nhftk12 · Jun 07, 2011 at 11:41 AM · degreescannonball

shoot cannon ball in 45 degrees

I made a tank with 3 weapons but I want to ask about the second: the cannons, and be ready for more questions about it if there will (it not right to ask something I didn't start yet...). On my tank there is 2 cannons that placed in 45 degrees on the rear.

This is the tank's cannon shooting function:

 function ShootCannons ()
  {
      if (canShootCannon)
      {
          var cannon1 = Instantiate(cannonBall, gameObject.Find("LCSpawnPoint").transform.position, transform.rotation);
          cannon1.gameObject.tag = "CharacterProjectile";
          cannon1.rigidbody.AddForce(transform.forward * 20000);
          yield WaitForSeconds (0.1);
          var cannon2 = Instantiate(cannonBall, gameObject.Find("RCSpawnPoint").transform.position, transform.rotation);
          cannon2.gameObject.tag = "CharacterProjectile";
          cannon2.rigidbody.AddForce(transform.forward * 20000);
          canShootCannon = false;
          yield WaitForSeconds (7.0);
          canShootCannon = true;
      }
  }

The cannons shoot the cannon balls always forward, it seems the cannon balls just spawn from the cannons and go not in 45 degrees like in should but in 0 degrees (forward). How to make the tank shoot in 45 degrees so it will seems I shoot from the cannons? I think I know where the problem is but I not sure how to fix this.

Sorry for poor english and if this is a bad question, I didn't find nothing about shooting in degrees...

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by nhftk12 · Jun 07, 2011 at 01:28 PM

Never mind, I found out alone:

 var cannon1 = Instantiate(cannonBall, gameObject.Find("LCSpawnPoint").transform.position, transform.rotation);
          cannon1.gameObject.tag = "CharacterProjectile";
          cannon1.rigidbody.AddForce(transform.forward * 2000);
          cannon1.rigidbody.AddForce(transform.up * 2000);
          yield WaitForSeconds (0.1);
          var cannon2 = Instantiate(cannonBall, gameObject.Find("RCSpawnPoint").transform.position, transform.rotation);
          cannon2.gameObject.tag = "CharacterProjectile";
          cannon2.rigidbody.AddForce(transform.forward * 2000);
          cannon2.rigidbody.AddForce(transform.up * 2000);
          canShootCannon = false;
          yield WaitForSeconds (7.0);
          canShootCannon = true;
Comment
Add comment · Show 3 · 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
avatar image flaminghairball · Jun 07, 2011 at 01:36 PM 0
Share

You can achieve the same effect with cannon1.rigidbody.AddForce((transform.forward + transform.up) * 2000);

avatar image Tom 17 · Jun 07, 2011 at 02:56 PM 0
Share

You didn't normalize the direction and just added the two vectors. If you're just guessing the force anyway this is propably of no concern, though you should know that the resulting force is not 2000 units. The solution of Bunny83 is the correct solution.

avatar image Nikhil11 · Feb 02, 2018 at 04:56 AM 0
Share

Thanks this helped me lot

avatar image
2

Answer by Tom 17 · Jun 07, 2011 at 11:53 AM

(Quaternion.AngleAxis(45,transform.right) * transform.forward) * 20000

or

(Quaternion.AngleAxis(-45,transform.right) * transform.forward) * 20000

try those

EDIT: rearranged terms after testing with compiler, sorry for any inconvenience

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
avatar image
1

Answer by Bunny83 · Jun 07, 2011 at 02:11 PM

I'm a bit confused, to which GameObject is that script attached? If it's the tank, you shouldn't use the tank's transform for the shooting. I see you use a spawnpoint GO, if you rotate the apawnpoint so that it's z axis (forward) points into the right direction you can use this transform.forward. That would be the "usual" way.

 var leftCannon : Transform;
 var rightCannon : Transform;
 
 function Start()
 {
     leftCannon  = gameObject.Find("LCSpawnPoint").transform;
     rightCannon = gameObject.Find("RCSpawnPoint").transform;
 }
 
 function ShootCannons ()
 {
     [...]
     var projectile1 = Instantiate(cannonBall, leftCannon.position, leftCannon.rotation);
     projectile1.rigidbody.AddForce(leftCannon.forward * 2000);
     
     // or since the projectile is rotated the same way:
     projectile1.rigidbody.AddForce(projectile1.transform.forward * 2000);
     [...]
 }
Comment
Add comment · Show 2 · 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
avatar image nhftk12 · Jun 07, 2011 at 02:18 PM 0
Share

dude, I solved the problem allready...

avatar image Bunny83 · Jun 07, 2011 at 02:40 PM 1
Share

But not in a very effective way... the angle is just the result of two forces. I don't care if you solved it. This page is a collection of questions/answers for the community.

avatar image
0

Answer by flaminghairball · Jun 07, 2011 at 01:35 PM

What happened is that you are shooting off the cannon ball in the tank's forward direction, which would most likely not be a 45 degree rotation.

Another note: You will probably want to cache a reference to the SpawnPoint transforms instead of using gameObject.Find(). GameObject.Find() is a search function, and takes more time than just using a reference, i.e:

 var lcSpawnPoint : Transform; //assign this in the inspector
 var rcSpawnPoint : Transform; //assign this in the inspector

And then just drop in those references instead of the Find() call.

Comment
Add comment · Show 1 · 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
avatar image nhftk12 · Jun 07, 2011 at 01:55 PM 0
Share

ty, it works.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.Sin() & Mathf.Cos() 1 Answer

Rotations problem.. 1 Answer

Real pivot rotation of all vertices in a mesh? 2 Answers

put a range between two degrees into code ? see image . 2 Answers

360 Angle Camera Rotate 2 Answers


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