• 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 DiscGolfer17 · Feb 19, 2013 at 01:22 AM · rotationgameobjecttransformjoystickcontrols

Control Ship and Gun Separately

I currently have ship orbiting a planet controlled by my left joystick. I have a gun (which is a child of ship in the hierarchy) that is controlled by the right joystick. When the ship isn't moving the gun can shoot in the direction that the joystick is pointing just fine. Now when I start controlling the ship, it also rotates the gun as well which makes the shooting direction change, even though I didn't move the right joystick.

Does anyone know how I can have the two objects rotate independently?

Let me know if you need more info.

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 robertbu · Feb 19, 2013 at 01:45 AM 0
Share

The ship can turn, and the ship can move. Should the gun maintain it target for both types of movement or just if the ship turns?

avatar image DiscGolfer17 · Feb 19, 2013 at 01:51 AM 0
Share

Yes, so as the ship is moving freely, the gun should stay pointing/shooting in the direction that the joystick is facing. Since the ship is orbiting, that seems to be what is throwing me off haha

2 Replies

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

Answer by Professor Snake · Feb 19, 2013 at 02:00 AM

Instead of having the turret be a child of your ship, you should have the ship be a child of your turret, and attach a kinematic rigidbody to it (the ship). Rotating the turret should be pretty straightforward now that it's the parent, but your movement code should not alter the turret's rotation. Now, in order to properly rotate the ship, you can attach this to the ship gameobject (which, again, should be a child of the turret):

 var rotationToHold:Quaternion;
 if(rigidbody.velocity.magnitude>0.1){//if we are moving
 transform.rotation=rigidbody.velocity; //make the ship's rotation face its movement
 rotationToHold=transform.rotation; //store this frame's rotation
 }
 else{
 transform.rotation=rotationToHold; //if not moving, keep the rotation we had on the last frame that we were moving
 }

Here is a visual representation:

 -Turret (moves with left analog stick, rotates with right analog stick, movement does not affect rotation)
    -ship (does not rotate based on input but based on velocity and is a child of turret)



Edit: Perhaps it is a fairly complex solution that would require you to radically change the way your objects are set up. I am also pretty sure that there is a much more simple way to do this that i am overlooking at the moment.

Edit 2: How can i be so silly, ignore the setup and use the code that i gave you for the turret, but instead of velocity you should check whether there is right thumbstick input.

 if(/*multiple Input.GetAxis checks here to see if there is right thumbstick input*/)
 rotationToHold=transform.rotation;
 else{ //if no right thumbstick input
 transform.rotation=rotationToHold;
 }

I am keeping my original answer up in case it suits you better though.

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 DiscGolfer17 · Feb 19, 2013 at 02:27 AM 0
Share

Thanks! $$anonymous$$aking the ship a child of the Gun worked, and I had to apply a rotation fix script to it as @robertbu suggested... I appreciate the help!

avatar image
0

Answer by robertbu · Feb 19, 2013 at 02:09 AM

Your answer didn't really address my question, but I'll make a couple of suggestions. One solution is to not make the gun a child of the ship, but to have the gun move with the ship. An easy way to accomplish this relationship is to:

  • Place an empty game object at the center of the ship (don't make it a child).

  • Place the gun where it needs to be in relationship to the ship

  • Make the gun a child of the empty game object

  • Add a script to the empty game object that moves it to the same position as the ship every frame.

This will leave the rotation of the gun the same in world space, but you will see the gun point at new object as the ship is translated. If you want the gun to remain on target even when the ship moves, you can use Transform.LookAt(), but I'm not sure how you would marry joystick position to LookAt() movement.

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 DiscGolfer17 · Feb 19, 2013 at 02:26 AM 0
Share

Sorry for the misunderstanding of your question, I tried this and it worked... sort of, as I move around the planet the bullet values get swapped for some reason, must be the LookAt() values I'm trying. Thanks again for the thoughts. I got it to work by making the Ship a child of the Gun as @Professor Snake's answer specified, and I created a script to fix the rotation. I'm going to have to give the answer to him this time around, although both of your answers were very helpful.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Neutralising child rotations -> Parent 1 Answer

Adding Rotation to a Gameobject 1 Answer

Rotating an object in position 2 Answers

How to rotate the game object 0 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