• 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
Question by Quelfth · Sep 10, 2020 at 02:36 AM · rotationtransformquaternion

Simplify this code

 GameObject a = new GameObject();
 GameObject b = new GameObject();
 a.transform.position = transform.position;
 b.transform.parent = a.transform;
 b.transform.localPosition = Vector3.zero;
 b.transform.localRotation = Quaternion.identity*Quaternion.Euler(180, 90, 90);
 a.transform.LookAt(target.transform);
 transform.rotation = b.transform.rotation;
 Destroy(a);
 Destroy(b);

The purpose of this code is to rotate a transform to point at an object. I want it to be as if the parent of the transform looked at the object, and the child had a local rotation of (180, 90, 90). This code does what I want, however, it creates and destroys two game objects every frame, which I'm certain is unnecessary. So, I would greatly appreciate if anyone could tell me how to rewrite this code so that it doesn't create any game objects.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Namey5 · Sep 10, 2020 at 05:32 AM

One trick when working with quaternions is to just think of them as rotations - when multiplying by a quaternion (from right to left), you are simply performing a rotation.

 //Find the rotation from our transform in the direction of the target
 Quaternion rot = Quaternion.LookRotation (target.transform.position - transform.position);
 //Set the rotation to (180,90,90) and then rotate by the look rotation
 transform.rotation = rot * Quaternion.Euler (180, 90, 90);
Comment
Bunny83

People who like this

1 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 Quelfth · Sep 10, 2020 at 03:11 PM 0
Share

What I missed yesterday was that the rot quaternion needs to be on the left of my (180, 90, 90) quaternion.

avatar image

Answer by Aviryx · Sep 10, 2020 at 03:04 AM

without creating gameobjects at runtime? I mean you would just have the two cubes already created and reference them?


 public GameObject cube1;
 public GameObject cube2;

If you're problem is running the code inside Update you could do something like this: (You could either do something to the cubes in Update after the gameobjects are created, or invoke a method that does something)


 public class SomeClass : Monobehaviour
 {
     private bool hasCreatedGameObjects;

     void Update()
     {
         if (!hasCreatedGameObjects)
         {
              hasCreatedGameObjects = true;

              GameObject a = new GameObject();
              GameObject b = new GameObject();

              DoSomethingWithCubes(); // call a method that does something to the cubes
         }

          // or do something with cubes per frame
     }
 }

Comment

People who like this

0 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 Quelfth · Sep 10, 2020 at 04:28 AM 0
Share

No, it's not that I'm trying to avoid creating them at runtime, I'm trying to avoid them existing at all.

You've failed to answer my question for two reasons: 1) You're still creating game objects. 2) You're code doesn't do what mine does.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Rotation Numbers Are Off? 0 Answers

How to find a rotation of a child object 0 Answers

What is the rotation equivalent of InverseTransformPoint? 4 Answers

How can I multiply the rotation of a mirrored object? 1 Answer

How to instantiate on custom rotation? 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