0
\$\begingroup\$

Not to look the character/camera direction but to look to the character/camera.

This is a screenshot of the object I'm rotating. And this is the face of the object and he is look at the camera(Looking at me).

The object is a child of a Camera and the Camera is child of a RigidBodyFPSController (1)

In the top of the script:

public float rotationSpeed;
private float x;
private int damping = 2;

In Update:

void Update()
    {
        if (Input.GetKey(KeyCode.R))
        {
            x += Time.deltaTime * rotationSpeed;
            objectToScale.transform.rotation = Quaternion.Euler(0, 0, x);
        }
        var lookPos = transform.position - objectToScale.transform.position;
        lookPos.y = 0;
        var rotation = Quaternion.LookRotation(lookPos);
        objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, Time.deltaTime * damping);
    }

Now when I press on R the object name NAVI is rotating but when I move the MainCamera around the NAVI object is not facing to me.

This is a screenshot when running the game first time the object is facing to me:

Face

But when I'm pressing on R and the object is rotating it looks like the object is facing to the left and I see the object side:

Face

Then I move the camera to the right he is rotating like I wanted facing to me:

Face

And if I move the camera to the left when the object is rotating I see the object back:

Face

But I want that each time the object is rotating that he will be facing to me.

The last screenshot showing the Hierarchy the RigidBodyFPSController (1) and the MainCamera as child and the NAVI as child of the MainCamera.

The script Change Scale is attached to the MainCamera and with this I'm rotating the NAVI.

Face

The main goal what I want is simple. When I press on R rotate the object and always make the object facing to me.

I tried now in the Update when pressing R to do:

if (Input.GetKey(KeyCode.R))
        {
            x += Time.deltaTime * rotationSpeed;
            objectToScale.transform.rotation = Quaternion.Euler(0, 0, x);
            objectToScale.transform.LookAt(Vector3.forward);
        }

But now it's not rotating at all.

\$\endgroup\$
  • 1
    \$\begingroup\$ Look at the Transform.LookAt method instead. \$\endgroup\$ – Draco18s no longer trusts SE Dec 26 '17 at 23:06
  • \$\begingroup\$ @Draco18s I tried it now I added to the Update the line: objectToScale.transform.LookAt(Vector3.forward); But now it's not rotating at all. \$\endgroup\$ – jdoedoe doedoe Dec 26 '17 at 23:29
  • 1
    \$\begingroup\$ ....Vector3 worldPosition I highly doubt you want your drone to look towards the scene coordinates (0,0,1) (i.e. the origin). \$\endgroup\$ – Draco18s no longer trusts SE Dec 26 '17 at 23:40
1
\$\begingroup\$

Since you want the object to look at the camera at all times and you've already made it it's child. It seems a bit redundant to me that you want it to change it's rotation again to look at you. This is of course assuming that the rotation code (with the Key R) that you've written was simply a test and not for the final build. Anyway, from what I understand, this is what you want: The object should look at the camera. You have the ability to rotate the object on its Z axis.

So after changing a few lines:

public GameObject objectToScale;
public float rotationSpeed;

private int damping = 2;
public float x;
private bool keyPressed = false;

void Update()
{

    if (Input.GetKey (KeyCode.R)) {
        x += Time.deltaTime * rotationSpeed;
        objectToScale.transform.localRotation = Quaternion.Euler (0, 0, x);
        keyPressed = true;
    }
    if (Input.GetKeyUp (KeyCode.R)) {
        keyPressed = false;
    }

    if (!keyPressed) {
        Quaternion newRotation = Quaternion.identity;
        objectToScale.transform.localRotation = Quaternion.Slerp (objectToScale.transform.localRotation, newRotation, Time.deltaTime * damping);
        x = objectToScale.transform.localRotation.z;
    }
}

I hope you get this to work.

| improve this answer | |
\$\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.