0
\$\begingroup\$

In my scene, I have my character facing a certain direction, with a camera attached. Now, when I press play, for some reason, the character immediately faces the other way. I've tried turning the model and camera to face the opposite way of that, to make up for it, but it still faces that same direction.

I've had a look at the script for the camera, but I'm not sure what could be causing this to happen. There's no target assigned that should cause that reaction, as the camera is a child of the player.

Below is a copy of my script:

public class FirstPersonCamera : MonoBehaviour
{
    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    GameObject character;

    void Start()
    {
        character = transform.parent.gameObject;

        Debug.Log("Camera is operating");
    }

    void Update()
    {
        var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;

        transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = true;

        if (Input.GetKeyDown("escape"))
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

    }


}

Does anyone see what could be causing this issue? I've using Unity version 2019.2

I'm also including screenshots of the player's position, as well as the camera's.

Player:

Player's co-ordinates

Camera:

Camera's position

There's something in the script that is causing the character to turn around, as I placed the script on another model and it had the same effect P:

\$\endgroup\$
  • \$\begingroup\$ You are trying to rotate the camera and player both in a different direction and maybe that is causing the problem, I suggest you check on "transform.localRotation" line, try commenting it. \$\endgroup\$ – Kartik Shah Nov 10 at 9:28
  • \$\begingroup\$ @KartikShah I just tried it, however it had the same affect as when I tried commenting out the line below it too. The odd thing is, the camera is physically placed in the same direction as the player, so i'm really stumped as to why this is happening, when it wasn't an issue previously. \$\endgroup\$ – CraftyMaelyss Nov 10 at 10:59
  • \$\begingroup\$ What position is the camera at within the parent character's coordinate space? \$\endgroup\$ – DMGregory Nov 10 at 14:00
  • \$\begingroup\$ @DMGregory The camera is placed in front of the model's eyes, to replicate first person. I'm updating the question with screenshots to help show the details. \$\endgroup\$ – CraftyMaelyss Nov 12 at 1:18
  • \$\begingroup\$ Be sure to include numbers! \$\endgroup\$ – DMGregory Nov 12 at 1:20
0
\$\begingroup\$

I figured out what it is (with the help of an amazing dev friend of mine)

This line, right here, was causing the issue:

 transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);

Now I've deleted it, it stops the rotation issue. Essentially those two lines of code were telling the model to face a specific direction on Start, and while the controls work fine aside from that spin/rotation, I didn't want the game to start with the player facing the wrong way.

\$\endgroup\$
  • \$\begingroup\$ I told you the same buddy. \$\endgroup\$ – Kartik Shah Nov 14 at 12:13
  • \$\begingroup\$ @Karik I must have misunderstood you, because when I commented out only one of the lines, it didn't work. Still thank you for the advice, and sorry I'm such a dummy ^-^; \$\endgroup\$ – CraftyMaelyss Nov 14 at 12:26

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.