0
\$\begingroup\$

Actually I have came here after searching from past 5-6 hours. Actually I am making a small game in which I have a camera and a gameobject. The Game Object is place in the center of the scene at 0,0,0 . My scene doesn't include anything but a skybox. I wanted to move my camera with gyro input, I got that working. My problem is that I want my object which is at 0,0,0 is to face camera always. The camera is controlled by gyro so the object should always remain infront of the camera. I have taken a script from wiki.unity3d and it seems to be somewhat working. Actually I want an offset for the transform.lookAt or if it's not possible I'll require some solution for it.

public class CameraFacing : MonoBehaviour
{
    public Camera m_Camera;

    void Update()
    {
        transform.LookAt((transform.position) + m_Camera.transform.rotation * Vector3.forward,
                m_Camera.transform.rotation * Vector3.up);
    }
}

Actually I have tried 3rd person and FPS shooter camera and Camera Follow script as well besides knowing it doesn't fit my need. As In most cases we want the camera to follow object. But in this case my input is through gyro of mobile device and I want the center object to always remain in front of camera.

Here is the code for gyroscope. I have made the object which I want to always stay infront of camera as suggested by comment but the camera rotate with gyro but not the object that is child of it, which is pretty strange.

private float initialYAngle = 0f;
private float appliedGyroYAngle = 0f;
private float calibrationYAngle = 0f;
void Start()
{
    Input.gyro.enabled = true;
    Application.targetFrameRate = 60;
    initialYAngle = transform.eulerAngles.y;
}

void Update()
{
    ApplyGyroRotation();
    ApplyCalibration();
}

public void CalibrateYAngle()
{
    calibrationYAngle = appliedGyroYAngle - initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time.
}

void ApplyGyroRotation()
{
    transform.rotation = Input.gyro.attitude;
    transform.Rotate(0f, 0f, 180f, Space.Self); // Swap "handedness" of quaternion from gyro.
    transform.Rotate(90f, 180f, 0f, Space.World); // Rotate to make sense as a camera pointing out the back of your device.
    appliedGyroYAngle = transform.eulerAngles.y; // Save the angle around y axis for use in calibration.
}

void ApplyCalibration()
{
    transform.Rotate(0f, -calibrationYAngle, 0f, Space.World); // Rotates y angle back however much it deviated when calibrationYAngle was saved.
}
\$\endgroup\$
  • \$\begingroup\$ Have you tried making the GameObject a child of the camera? That way when the Camera moves the GameObject will automatically move with it \$\endgroup\$ – hobnob Nov 22 '16 at 17:11
  • \$\begingroup\$ tried that but the gameobject doesn't remain in view of camera. As camera input is controlled through gyroscope. \$\endgroup\$ – Adnan Nazir Nov 22 '16 at 17:12
  • \$\begingroup\$ Have you got the code you're using to move the camera? If the GameObject is a child then it should just move with the camera as it's transform is changed. As it's not, the problem might be the way you've implemented the movement. \$\endgroup\$ – hobnob Nov 22 '16 at 17:16
  • 1
    \$\begingroup\$ No question is lame. Even questions that have seemingly simple solutions will help others with the same or similar problem :-) If you really want a question deleted though it might be worth flagging it for moderation maybe? Or marking closed/answered if you can \$\endgroup\$ – hobnob Nov 22 '16 at 18:07
  • \$\begingroup\$ I have edited my question with the code used to move camera with gyro which I think is problematic. \$\endgroup\$ – Adnan Nazir Nov 23 '16 at 7:37
1
\$\begingroup\$

While I agree that making your object a child of the camera should work (though it won't stay at 0,0,0); If you want to keep the object at it's current position and only adjust it's rotation to face the camera, you can use LookRotation to get the behavior you want.

To do so simply set the center object's rotation equal to Quaternion.LookRotation which takes one or two parameters. The first is the position to look at relative to the position of the object. To get this simply subtract the target position from the object's position. The second is the up direction, which you can ignore unless the camera also rolls around the Z from your gyro movement and you want the object to match that, in which case you can just pass it the .up from your camera's transform.

In the end you should end up with something like this:

transform.rotation = Quaternion.LookRotation(m_Camera.transform.position - transform.position);

or

transform.rotation = Quaternion.LookRotation(m_Camera.transform.position - transform.position, m_Camera.transform.up)

Note that neither of these will move the object, so if you rotate your camera without moving it in space to keep it facing the object the object will leave the center of your screen when you turn the camera!

If you want the object to stay in the center of the screen upon rotating the camera then it's impossible to leave it at 0,0,0 without major shenanigans and making it a child object remains your best option (any setting of the position by you would just be duplicating a good chunk of what making an object a child does already)

If making the object a child of the camera doesn't seem to be working, make sure it's parented to the gameobject that's actually rotating and not a container object which only shifts position. This is an easy mistake to make that's tripped me up before when working with camera objects where the object with the actual camera component was actually one or two children down in the hierarchy.

\$\endgroup\$
0
\$\begingroup\$

Well I think I was overthinking as I was doing it for VR Headset so I was thinking there should be some really complex thing to achieve this. But this is super simple. You need to make the object child of your camera and keep in mind that you need to position it locally. Thanks.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.