- Home /
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;
}
}
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...
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);
}