Juan
Juan

Reputation: 1

How to attach the position/rotation of a GameObject to the position/rotation Camera

I'm working on the kinect V2, and i would like to make a script witch attaches the position of a game object ( moving with a head tracker) and the camera of my scene, to make an illusion of hologram.

I access it with the camera as a child of the game object, a lookat for a camera orbital ( rotation ), and a projection matrix for the camera to flip some axis of the camera.

But the projection matrix make some bugs with the textures and lighting of my scene. That´s why I would like to create a c# script who say:

Position camera = position gameobject (x,y,-z)

If position gameobject x>0 Else rotation camera y increase.

If position gameobject x<0 Else rotation camera y decrease.

If position gameobject y>0 Else rotation camera y decrease.

If position gameobject y<0 Else rotation camera y increase.

Nothing to do for the rotation camera z axis.

Can you help me to traduce this in C# ?

This is not my script :) but only the main idea i would like to do. I hope receive your help for a beginner developer!

Thank´s a lot

Upvotes: 0

Views: 1879

Answers (1)

marsh
marsh

Reputation: 2730

It is kind of hard to tell what you want. Here is some psuedo code that should help you on your way. I do not have unity to test it and it is not complete. But it will give you the basics to learn what you are trying to do.

using UnityEngine;
using System.Collections;

public class TestScript: MonoBehaviour {

    public GameObject camera;
    public GameObject gameObject;
    public int rotationAmount = 1;
    // Update is called once per frame
    void Update () {

        Vector3 camera = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, -gameObject.transform.position.x);

        if(gameObject.transform.position > 0) {
            camera.x -= rotationAmount;
        }

        camera.transform.position = camera;
    }
}

You will have to name the C# script TestScript drag it on a GameObject and drag your camera and gameObject into its slots. If you do not know how to do this I suggest you read into Unity more.

Upvotes: 1

Related Questions