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