Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 8r3nd4n · Jan 27, 2015 at 07:07 AM · camerarotationvector3lookatitween

Get a child object to rotate a parent to the cameras direction

I am building an application that allows the user to customise various sporting balls to their liking. So far I have it so that a person can change the colours of different parts, upload their own team/company logo and rotate around the ball. What I am trying to achieve but not sure how to is when a user presses a particular logo button (EG Logo Button 1), that logo spins to face the camera. I can achieve this by setting it so when the button gets pressed, it rotates the ball to a predetermined vector3. If you press the logo buttons on the side you will see what I mean:

http://rhinoaustralia.com/custom-netball-designer/index.html

What I also want though is for the user to be able to rotate around the ball and retain this functionality. At the moment, if you rotate around the ball with the right mouse button, when you press a logo button it goes to world Vector 3 coordinates set out in the script as opposed to those relative to the cameras position and rotation.

The way that the scene is set up is that there is a ball mesh and it has 3 child meshes that sit just above it that contain the logo material. I was thinking of using transform.LookAt() on the children but this wouldn't move the parent too and is probably not the best solution for this. Possibly I should store the cameras vector3 and pass it to this function, then update the new vector3 here by adding in that of the passed camera?

Advice appreciated as always.

 using UnityEngine;
 using System.Collections;
 
 public class DrawLogoGUI : MonoBehaviour {
 
     public ChangeLogo logo1;
     public ChangeLogo logo2;
     public ChangeLogo logo3;
 
     //Check Buttons gets called from another script when the user presses the appropriate button
     public void CheckButtons(string n)
     {
         if(n == "Logo1")
         {
                     //This needs to rotate according to the cameras rotation.
             iTween.RotateTo(this.gameObject, new Vector3(0,270,-90),1.0f);
             logo1.enabled = true;
             logo2.enabled = false;
             if(logo3 != null)    logo3.enabled = false;
         }
         if(n == "Logo2")
         {
             iTween.RotateTo(this.gameObject, new Vector3(-90,90,0),1.0f);
             logo1.enabled = false;
             logo2.enabled = true;
             if(logo3 != null)logo3.enabled = false;
         }
         if(n == "Logo3")
         {
             iTween.RotateTo(this.gameObject, new Vector3(270,180,90),1.0f);
             logo1.enabled = false;
             logo2.enabled = false;
             if(logo3 != null)logo3.enabled = true;
         }
     }
 
     public void ResetButtons()
     {
         logo1.enabled = true;
         logo2.enabled = true;
         if(logo3 != null)logo3.enabled = true;
     }
 }
 

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 Imtiaj Ahmed · Jan 27, 2015 at 12:45 PM 0
Share

Well, I read through several times to understand what you want. And I assume, you want the logo(s) to face the camera when clicking the logo1,2,3 buttons. Well, what I would do is first, calculate the relative distance from the ball to the camera and then the required rotation of the ball to face the camera. And leave the child logo objects alone to not rotate their parent. And then, not stick to a fixed vector3 rotation angle with iTween ;). Here are the codes that may help you -

 Vector3 relativePos = theCamera.transform.position - theBall.transform.position; // Calculate differences
 Quaternion desiredRotation = Quaternion.LookRotation (relativePos); // Calculate required rotation angle
 theBall.transform.rotation = Quaternion.Lerp (theBall.transform.rotation, desiredRotation, smoothTime); //for the itween thingy


Or, you can use iTween if you are not comfortable with Lerp.

 iTween.RotateTo (theBall, desiredRotation.eulerAngles, 1.0f);

And for the log2/3, just add required rotation vector to desiredRotation.

 desiredRotation = Quaternion.Euler(desiredRotation.eulerAngles + yourVector3); // yourVector3 is for your logo2/3

Hope it helps...

avatar image 8r3nd4n · Jan 28, 2015 at 04:02 AM 0
Share

Thanks, You were right in understanding what I meant. After reading it again it is a bit confusing. The top part for logo 1 works great and the itween part does, but the other logos are problematic. The more the camera moves around, the ball doesn't rotate to face the camera properly.

$$anonymous$$aybe I have put it in wrong?:

 if(n == "Logo2")
         {
             Vector3 relativePos = transform.position - cam.transform.position; // Calculate differences
             Quaternion desiredRotation = Quaternion.LookRotation (relativePos); // Calculate required rotation angle
             desiredRotation = Quaternion.Euler(desiredRotation.eulerAngles + new Vector3(40,180,260));                
             iTween.RotateTo(this.gameObject, desiredRotation.eulerAngles,1.0f);
 
         }

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

How do i lock the position of the camera above the player relative to the origin point? 0 Answers

RTS Camera movement wrong after rotation 1 Answer

Movement relative to Camera and XZ Plane 1 Answer

Adjust camera rotation while looking at a target 1 Answer

How can I always make a sphere appear to be rotating in the same direction no matter the camera 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