• 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 madkar3000 · Jun 24, 2020 at 07:07 PM · rotationquaternionx-axisrotatetowardsfreezerotation

How to constantly rotate an object to 0f by X-axis?

Hello Everyone! Newbie question. How to make an object to rotate to 0.0f by X-axis permanently. I mean if it changes only by X-axis then the object automatically and smoothly will rotate to 0f and stops there until another incline. I am doing a tank movement and I did script wherein every time the tank moves forward or backward it inclines and Rotation by X-Axis goes less than 0f. I can't freeze rotations in "Rigidbody/constraints" because it is a child of a blank object with Rigidbody and Box Collider.

I can't use Quaternion.RotateTowards because I don't want my object to change its Y and Z-axis. I will appreciate help with C# language!

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 madkar3000 · Jun 25, 2020 at 12:01 AM 0
Share

public class Tank$$anonymous$$ovement : $$anonymous$$onoBehaviour { Quaternion OriginalRotation; private Rigidbody rb; public Transform tank; private float TurningRate = 10f; private Quaternion rotationGoal = Quaternion.Euler(0f, -178.511f, 0f); private Vector3 RotateToTarget; private void Start() { OriginalRotation = tank.rotation; rb = GetComponent(); } private void Update() { var velositi = transform.InverseTransformDirection(rb.velocity); if (Input.GetKey(KeyCode.W)) { rb.AddForce(gameObject.transform.forward 290f, Force$$anonymous$$ode.Force); if (velositi.z < -10) tank.Rotate(1f, 0f, 0f); } else if (Input.GetKey(KeyCode.S)) { rb.AddForce(gameObject.transform.forward -290f, Force$$anonymous$$ode.Force); if (velositi.z > 10) tank.Rotate(1f, 0f, 0f); }

     Debug.Log(velositi.z);
     if (velositi.z > 1)
     {
         tank.Rotate(-0.5f, 0f, 0f); 
     }
     else if (velositi.z < -1)
     {
         tank.Rotate(-0.5f, 0f, 0f);
     }
     if (velositi.magnitude < 1)
     {
         tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
     }
         
     else if (velositi.magnitude > -1)
     {
         tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
     }
         

     float horizontal$$anonymous$$ = Input.GetAxis("Horizontal");
     if (horizontal$$anonymous$$ > 0)
        gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 100f);
     if (horizontal$$anonymous$$ < 0)
        gameObject.transform.Rotate(Vector3.down * Time.deltaTime * 100f);
 }

}

avatar image madkar3000 · Jun 25, 2020 at 12:02 AM 0
Share
 private Rigidbody rb;

 public Transform tank;
 private float TurningRate = 10f;
 private Quaternion rotationGoal = Quaternion.Euler(0f, -178.511f, 0f);
 private Vector3 RotateToTarget;
 private void Start()
 {
     OriginalRotation = tank.rotation;
     rb = GetComponent<Rigidbody>();
 }
 private void Update()
 {
     var velositi = transform.InverseTransformDirection(rb.velocity);
     

     if (Input.GetKey(KeyCode.W))
     {
         rb.AddForce(gameObject.transform.forward * 290f, Force$$anonymous$$ode.Force);
         if (velositi.z < -10)
             tank.Rotate(1f, 0f, 0f);
     } 
     else if (Input.GetKey(KeyCode.S)) 
     {
         rb.AddForce(gameObject.transform.forward * -290f, Force$$anonymous$$ode.Force);
         if (velositi.z > 10)
             tank.Rotate(1f, 0f, 0f);
     } 

     Debug.Log(velositi.z);
     if (velositi.z > 1)
     {
         tank.Rotate(-0.5f, 0f, 0f); 
     }
     else if (velositi.z < -1)
     {
         tank.Rotate(-0.5f, 0f, 0f);
     }
     if (velositi.magnitude < 1)
     {
         tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
     }
         
     else if (velositi.magnitude > -1)
     {
         tank.rotation = Quaternion.RotateTowards(transform.rotation, rotationGoal, TurningRate);
     }
         

     float horizontal$$anonymous$$ = Input.GetAxis("Horizontal");
     if (horizontal$$anonymous$$ > 0)
        gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 100f);
     if (horizontal$$anonymous$$ < 0)
        gameObject.transform.Rotate(Vector3.down * Time.deltaTime * 100f);
 }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tuinal · Jun 25, 2020 at 04:01 AM

If you're using physics it's best not to mess around with transforms. Either you go all-in on a physics based controller, or not. If you change transforms, rigidbodies are prone to catapulting around if they move inside a collider. This can be avoided with onerous coding but it's rarely worth the effort.

For a physics-based tank, use e.g. a capsule collider for each track (you can attach multiple colliders to a single gameobject - if you just use a box it will be prone to getting stuck on uneven terrain), and AddTorque or AddForce to it's rigidbody to move it rather than changing the transform. If you need snappier, less realistic movement, set rigidbody.velocity and rigidbody.moveRotation in FixedUpdate rather than transform properties.

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 N-8-D-e-v · Jun 24, 2020 at 07:30 PM

Rotate your object by changing EulerAngles like so

 transform.rotation = Quaternion.Euler(0, whatever number, whatever number);
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 madkar3000 · Jun 25, 2020 at 12:43 AM 0
Share

Didn't work quite well, now it is not rotating at all, where should I put it? (I have sent the code)

avatar image N-8-D-e-v madkar3000 · Aug 28, 2020 at 01:33 PM 0
Share

put it in lateupdate

avatar image N-8-D-e-v madkar3000 · Aug 28, 2020 at 01:34 PM 0
Share

it should actually look like this transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, transform.eulerAngles.z);

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

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

Related Questions

How do I make an angle with the X axis, Y axis, and Z axis and not rotate AROUND the axis? 0 Answers

RotateTowards won't rotate in X axis 0 Answers

Longest distance rotation problem: From -175 to 175 via 0 using Quaternion.RotateTowards stops at 5, why? 2 Answers

Child transforms screwed up by parent's rotations 1 Answer

How to rotate towards the mouse on one axis while snapping 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