Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 $$anonymous$$ · Oct 19, 2015 at 01:30 PM · rotationrigidbodyquaternion

Barrel rotation Y, not working well with rigidbody.

Basically I have a barrel with a rigidbody and a sphere collider attached to it. What I'd like to achieve is, when the barrel moves forward, it rotates around the Z axis as a barrel would in real life. That's easily achieved by wether a forward wheter a backward force.

BUT the problem comes when I'd like to rotate it around the Y axis, so I can turn with it. I was trying to chang it's rotation by having a rotY variable, which I increase or decrease depending on which direction to turn, then I convert that into a Vector3 which then I convert to a Quaternion with EularAngles and lastly I change the transform's rotation. It rotates to left and right but then the rotation to the Z axis doesn't work..

I was also trying with rigidbody.MoveRotation() but couldn't really figure it out how should it work for me perfectly. Anyone knows the solution ?

Here's my code:

 public class Movement : MonoBehaviour {
     private Rigidbody rb;
     public float forward, backward, rotY;
     public Vector3 rotation;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
         rotY = 90.0f;
     }
     
     // Update is called once per frame
     void Update () {
         //Moving with keys
         if (Input.GetKey(KeyCode.UpArrow)) {
             rb.AddForce(Vector3.forward * forward, ForceMode.Acceleration);
         } else if (Input.GetKey(KeyCode.DownArrow)) {
             rb.AddForce(Vector3.back * backward, ForceMode.Acceleration);
         }
         if (Input.GetKey(KeyCode.LeftArrow)) {
             rotY -= 1.0f;
         } else if (Input.GetKey(KeyCode.RightArrow)) {
             rotY += 1.0f;
         }
 
         if (rotY > 360.0f || rotY < -360.0f) {
             rotY = 0.0f;
         }

         rotation = new Vector3(rb.rotation.x, rb.rotation.y + rotY, rb.rotation.z);
 
         //Rotation
         rb.rotation = Quaternion.Euler(new Vector3(rb.rotation.x, rotation, rb.rotation.z));
     }
 }
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

2 Replies

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

Answer by toromano · Oct 19, 2015 at 01:46 PM

You could try giving torque in global up axis for steering:

 void FixedUpdate()
 {
     //steering
     float turn = Input.GetAxis("Horizontal");
     rb.AddTorque(Vector3.up * speed * -turn);

     //driving
     float throttle = Input.GetAxis("Vertical");
     rb.AddTorque(transform.up * speed * throttle);
 }

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 $$anonymous$$ · Oct 19, 2015 at 02:26 PM 0
Share

Works lik a charm if I only use the //steering part. Thank you !

avatar image
0

Answer by RChrispy · Oct 19, 2015 at 01:49 PM

I think this is 3D? :D

And you rotate the local axis? Of your model? If you use a 3d Model parent this model under an empty ( if you didnt do it) its just better to do so because later changes with the models are safer that way. Another reason is if you use blender it will get problematic.

I would Make an empty with the rotation: 0 0 0 the model with: 90 0 0 (blender cylinder parented at empty)

And rotating the Z axis only: Quaternion.Euler(0f,0f, rotY);

Hope this helps you a little maybe you can describe your setup a little bit more if this doesn't help ;)

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 $$anonymous$$ · Oct 19, 2015 at 02:27 PM 0
Share

I don't see why are you being cynical. $$anonymous$$aybe you just can't express yourself in a good way. Also if I didn't mention any 2d components like RIgidBody2D, and I was using Vector3-s ins$$anonymous$$d of Vector2-s, why wouldn't you assume it's in 3D ?

avatar image RChrispy $$anonymous$$ · Oct 20, 2015 at 06:38 AM 0
Share

Dont know what you mean I am not a native english speaker and I just made my points clear. Told you about better practice not only how to fix the problem. But you can just pretend to see insults and ignore it. I dont see any special problems in the text above just help and advice to setup your things differently because later on you get problems.

ps: And yeah I overlooked the rigidbody component and therefore the question for 3d this was no mean question at all, you should be more chill^^.

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

34 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 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

why can't my Rigidbody not rotate on slopes? 1 Answer

Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle) 1 Answer

Rotate Rigidbody to always face a force 0 Answers

transform.Rotate and Rigidbody.MovePosition 1 Answer

Rigidbody wont rotate in the other direction. 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