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 marcuscodes · Mar 25, 2012 at 05:31 PM · rotatecannon

cannon aim problem

                                  ********Solved******** Thanks!

Question: how to get a good looking cannon with the body only moving in y axis and cannon moving in x axis.

Problem: Unity and 3ds max had problem with x,y,z.

Fix: Create parents to the parts and make the parent rotate.

here is the code i used. the cannon is only an illusion that its only moves on the x axis:

 var target:Transform;
 var cannon = false;
 var body = false;
 
 function Update () {
     if (target) { // end early if no target
         if (Vector3.Distance(transform.position, target.position) > 20) { // if target is to far start rotate like a radar
             transform.Rotate(Vector3.up * (Time.deltaTime *100), Space.World); 
         } else { // else set new rotation depending on what object the script is set on
         var new_rotation = Quaternion.LookRotation(target.position - transform.position);
         if (cannon) {
            // if you want it to look good and same time work don't lock any axis here!
         }
         if (body) {
             new_rotation.x = 0; // lock x
             new_rotation.z = 0; // lock z
         }
         transform.rotation = Quaternion.Slerp(transform.rotation, new_rotation, Time.deltaTime * 2); // smoot look at
         }
 
     }
 }
Comment
Add comment · Show 2
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 BerggreenDK · Mar 25, 2012 at 06:25 PM 0
Share

This might have something to do with how 3D $$anonymous$$ax and Unity sees axis. A lot of 3D programs does not agree upon whats Y and whats Z.

avatar image cowlinator · Mar 25, 2012 at 08:59 PM 0
Share

Yeah, look at your cannon-body, and make sure that the green y axis is in fact pointing up.

You might also try using Transform.Rotate(), and play with the second parameter (local space vs. world space)

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by brightlance · Mar 25, 2012 at 09:55 PM

I've recently done a similar thing in my project. In order to get the separate parts to rotate correctly, first rotate the body look at the target's position like you are already doing, but make it look at Vector3(target.x,body.y,target.z).

Doing this will stop the gun from aiming in weird angles.

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
0

Answer by CreativeStorm · Mar 25, 2012 at 10:29 PM

I had a very similar problem with my tank. The turret rotates automatically toward the given target and fires. So my turret was pointing upwards and the top was pointing towards the target. Its related to the axis differences between Unity and Max. I Just added the turret to an empty GO as a child. Now i rotate the Parent and everything is fine. For me this was the fastest solution - the are better ones for sure ;)

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 marcuscodes · Mar 27, 2012 at 04:49 PM 0
Share

yaay! it worked.. but still i cant make the cannon to rotate with the body.. when i try putting the cannon as a child of the body it goes nuts and starts to avoid me XD

avatar image
0

Answer by marcuscodes · Mar 27, 2012 at 05:25 PM

hmmm.. it still don't work :(

is this code right?

var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2);

and if you want to lock axels you just write rotation.x = 0; infront of the transform.rotation

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
0

Answer by CreativeStorm · Mar 27, 2012 at 05:51 PM

well for my tank i use two target .. kinda of - not right sure how to explain. When there is a target, it is the target. In other situations the target for the rotation is set to the rotation of the body... so it smoothly rotates to where it's supposed to at all times.

 var targetRotation : Quaternion;
     
     if(GunTarget != Vector3.zero)
     {
         targetRotation = Quaternion.LookRotation(GunTarget - thisTransform.position);
         MyTurret.rotation = Quaternion.Slerp(MyTurret.rotation, targetRotation, 9* Time.deltaTime);
     }
     else
     {
         targetRotation = thisTransform.rotation;
         MyTurret.rotation = Quaternion.Slerp(MyTurret.rotation, targetRotation, 1* Time.deltaTime);
     }
     
     MyTurret.eulerAngles.x = 0;


this is the code for my target its in an function called from the update function. The Turret is cached in "myTurret". "GunTarget" is a Vector3 position set for my by touching the screen - would be your player's position for you i assume - sry, i'm in a hurry. Hope i could help though

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

8 People are following this question.

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

Related Questions

Camera rotation around player while following. 6 Answers

making one object a parent of another via javascript 2 Answers

character and Camera rotate 2 Answers

rotate object to touch position 1 Answer

Click and rotate an object during the game 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