• 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 zastrow · Sep 23, 2014 at 12:54 AM · transform.rotate

Transform.Rotate in local space on one axis

I have a map that is locked to the gyroscope so world space coordinates are all over the place.

I'm trying to track and and follow an object using local space.

I also want to have a rotation speed, so I'm trying to use Quaternion.Slerp, but I just read that Quaternion is for world space only - probably the source of my problem.

      theDirection = (VBall.position - transform.position).normalized;
     theRotation = Quaternion.LookRotation(transform.position - VBall.position, theDirection);
     theRotation.y = 0f;
     transform.rotation = Quaternion.Slerp(transform.rotation, theRotation, Time.deltaTime * RotationSpeed);

I've tried many thing including transform.localPosition but nothing seems to work the way I want it to. This script will rotate around to the object, but it slerps from a coordinate that is world space, so it rocks on the Y axis as it makes the transistion to the new coordinates.

Question; Is there a way to Lerp or Slerp from on rotation to another rotation using only local coordinates?

Most people won't have this issue since the map is tied to a static world space coordinate.

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 zastrow · Sep 23, 2014 at 02:20 AM 0
Share

In my world X-axis is up and this snip of code will rotate an object clockwise.

this.transform.Rotate(Vector3.up Time.deltaTime 30f); //rotates clockwise.

It works perfectly, if there was a way to modify this to follow and object I might be set.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Sep 23, 2014 at 03:01 AM

Though I'm not 100% sure I fully understand, I'll take a shot at your problem:

 var pos = ProjectPointOnPlane(transform.up, transform.position, VBall.position);
 theRotation = Quaternion.LookRotation(pos, transform.up);
 transform.rotation = Quaternion.Slerp(transform.rotation, theRotation, Time.deltaTime * RotationSpeed);

 function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }

Note that your use of Slerp() here will produce an eased rotation. If you don't want eased, use Quaternion.RotateTowards() and adjust 'RotationSpeed' up.

This code works by constructing a mathematical plane on the local up running through this object and projecting the ball position on that plane. This new position, combined with specifying 'transform.up' in the LookRotation() will result in a rotation only on the local 'y' axis. I'm not sure what would happen if the ball were directly above or below this object, since this code would result in a Vector3.zero for the direction passed to LookRotation(). If it is possible in your game, you might have to test for the situation. That is only do the rotation if (pos - transform.position != Vector3.zero).

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 zastrow · Sep 23, 2014 at 09:56 PM 0
Share

I translated your code into C#. This script works on the positive side of the local X Axis but flips out on the negative side so only works for a positive 180 degrees field of view.

 using UnityEngine; using
 System.Collections;
 
 public class PrivaTank : $$anonymous$$onoBehaviour
 {
  
      public Transform VBall;
      private Quaternion theRotation;
      private float RotationSpeed = 2f;
      private Vector3 pos;
       void Update ()
      {
          pos = ProjectOnPlane(transform.up, transform.position, VBall.position);
          Debug.Log(pos);
          theRotation = Quaternion.LookRotation(pos, transform.up);
          transform.rotation = Quaternion.Slerp(transform.rotation, theRotation, Time.deltaTime * RotationSpeed);     
      }
  
      Vector3 ProjectOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point)
      {
          float distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
          return point + planeNormal * distance;
      } 
 }






avatar image zastrow · Sep 23, 2014 at 10:22 PM 0
Share

Perhaps a Vector3.Cross between global and local vectors of my target object...

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

25 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

Related Questions

jittery movement when using transform.up and transform.Rotate 0 Answers

How to instantiate an object with a set rotation in one axis, but the other two axes inherited from the parent? 1 Answer

transform.Rotate slows down my moving cube 0 Answers

Why does rotate angels does not match& 1 Answer

Transform.RotateAround Issues 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