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 JonnyHolmes · Jan 18, 2017 at 12:00 AM · rotationquaternionangleaxis

Problem rotating object around particular axis

I am trying to write a script which will rotate my 'grabber' object around its y-axis by 180 degrees when it collides with another object. The other objects are smaller objects moving along a conveyor belt. The grabber should grab onto an item, rotate around its y axis 180 degrees, and release the object.

alt text

My script successfully rotates by 180 degrees on the y axis every time it collides with a conveyor object, but at the same time changes the x and z axis rotations, which I do not want. The resulting rotation is this -

alt text

As you can see, the grabber is no longer tilted. How would I get the object to rotate ONLY on its y axis? So that no matter how it is oriented, it would always rotate on its local y axis by 180 degrees when it collides?

The most relevant part of the code below is -

  //Specify the desired rotation
              deltaRot = Quaternion.AngleAxis(transform.rotation.eulerAngles.y + 180f, transform.up);

My script-

 using UnityEngine;
 using System.Collections;
 
 public class MagnetScript : MonoBehaviour {
 
     //The grab positions
     private Transform[] grabPositions = new Transform[2];
     private Transform grabPosition; 
 
     //The obj to grab and it's rb
     private GameObject grabObj;
     private Rigidbody rb;
 
     //Our Finite State machine
     private enum State {idle, grabbing, rotating, releasing};
     private State state = State.idle;
 
     //Our grabber turn speed
     public float turnSpeed;
 
     //Orignal grabber rotation and desired rotation
     private Quaternion originalRotation, desiredRotation, deltaRot;
 
     //The layer mask to define
     int layerMask;
 
     float buffer = 0.01f;
 
     void Start()
     {
         //Define the two grab positions
         grabPositions[0] = transform.Find("GrabPosition1").transform;
         grabPositions[1] = transform.Find("GrabPosition2").transform;
 
         //Define the layer mask (conveyor grabObjects)
         layerMask = LayerMask.NameToLayer("ConveyorObjects");
     }
     void FixedUpdate()
     {
         if (state == State.grabbing) {
 
             if (Vector3.Distance (grabObj.transform.position, grabPositions [0].position) > Vector3.Distance (grabObj.transform.position, grabPositions [1].position)) {
                 grabPosition = grabPositions [1];
             } else
                 grabPosition = grabPositions [0];
 
             //Hold the grabObject in the grab position
             rb.MovePosition (grabPosition.position);
 
             //Specify the desired rotation
             deltaRot = Quaternion.AngleAxis(transform.rotation.eulerAngles.y + 180f, transform.up);
 
             //Set State to rotating
             state = State.rotating;
         }
 
         if(state == State.rotating)
         {
 
             //Hold the grabObject in the grab position
             rb.MovePosition (grabPosition.position);
 
             //Slerp the rotation
             transform.rotation = Quaternion.Slerp( transform.rotation, deltaRot , Time.deltaTime * turnSpeed);
 
             //Calculate the relative position to the og position
             Quaternion relative = Quaternion.Inverse(deltaRot) * transform.rotation;
 
             //if the relativePos is less than the threshold, then release the grabObject
             if ( relative.eulerAngles.y < buffer ) {
                 state = State.releasing;
             }
         }
 
         //Release the grabObject, turn gravity back on
         if (state == State.releasing){
             rb.useGravity = true;
             rb.isKinematic = false;
             state = State.idle;
             grabObj = null;
             grabPosition = null;
         }
     }
 
     void OnTriggerEnter(Collider col)
     {
         //Grab grabObject if we are not already grabbing something
         if (state == State.idle) {
 
             //Only grab the conveyor grabObjects
             if (col.gameObject.layer == layerMask) {
 
                 state = State.grabbing;
 
                 //Get a reference to the grabObject being grabbed
                 grabObj = col.gameObject;
 
                 //Get grabObjects rigidbody and turn off use gravity while we grab
                 rb = col.gameObject.GetComponent<Rigidbody> ();
                 rb.useGravity = false;
                 rb.isKinematic = true;
             }
         }
     }
         
 }

capture.png (29.1 kB)
capture2.png (43.0 kB)
Comment
Add comment · Show 1
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 QuentinLG · Jan 18, 2017 at 12:10 AM 1
Share

Did you try to use Transform.Rotate() ins$$anonymous$$d of modifying a Quaternion?

1 Reply

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

Answer by JonnyHolmes · Jan 18, 2017 at 09:25 PM

Great, thanks , got it working using

 //Calculate the relative position to the og position
 Quaternion relative = Quaternion.Inverse(originalRotation) * transform.rotation;
 
 //if the relativePos is less than the threshold, then release the grabObject
 
 transform.Rotate(0, Time.deltaTime * turnSpeed, 0, Space.Self);
 
 if (relative.eulerAngles.y >= 180f)
 {
     state = State.releasing;
 }`
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

object is not rotating from right direction 0 Answers

Finding roll angle 0 Answers

Quaternion.AngleAxis with transform.up unexpected behavior... 0 Answers

Rotation from AngleAxis() ToAngleAxis() flips back and forth 1 Answer

reproducing hingejoint.angle 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