SOLVED Camera turns around z axes by its own

Hi together,
I´m quite new to Unity and have a problem i could not get fixed. I´m trying to write a FPS-Controller, so I took the example from the character controller, dragged it to my cam and modified it to my needs:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float turnSpeed = 10f;
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;

    void Update()
    {
        CharacterController controller = GetComponent<CharacterController>();
        transform.Rotate(Input.GetAxis("Tilt Camera") * turnSpeed, Input.GetAxis("Turn Camera") * turnSpeed, 0);
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Strafe"), 0, Input.GetAxis("MoveForward"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if (Input.GetButton("Jump")) moveDirection.y = jumpSpeed;
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
}

I´ve added line 15 to realise a head-movement controlled by mouse.
It is working except one thing:

There is a rotation around the z-axes and I cant understand why this happens…

Is there anyone out there who can me explain what i`ve done wrong? I´ve tried to solve this ( or even to understand why this happens) for the last couple of hours…

Thx in advance

Wolfgang

When you rotate like this, you are rotating in the local axis.
The simplest way I’ve found to solve this problem as a beginner is to have the turning on the character itself like you probably had originally, and have an empty child object in the character specifically for its transform to handle the tilt.
The Camera will be a child object of the empty child object, or reference the empty child object instead of the character in however you are repositioning the camera.

...
public Transform tiltChild;//Assign this in the inspector. This is supposed to be the empty child object in the character

void Update()
{
    ...
    transform.Rotate(0,Input.GetAxis("Turn Camera") * turnSpeed, 0);
    tiltChild.Rotate(Input.GetAxis("Tilt Camera") * turnSpeed,0,0);
    ...
}
1 Like

Thanks mate, I believe this could work. I´ve to try it after work :slight_smile:

Do you have any ideas why this drift occure in my sample? I´ve got absolutly no idea why this doesnt work.

Greeting from Salzburg

Wolfgang

I already told you why in the first line of my previous reply

Thanks for your reply again…
I believe I have to look about local/global again. I thought the inspecter Shows me the local tranform??? And because I multiplied it by 0 it should be 0… but the inspector Shows me values in the Rotation on the z axes…

Thx for your answer

Wolfgang

Now it makes click in my brain… I just tried to simulate the movement with my fingers forming the coordinate-system … I was sooo blind :smile:

Thx a lot