0
votes

I made this script for my Player object. It has to child, 1 camera and 1 model. They problem is whenever i move my mouse the player moves down. and the cam goes up.

Script:

public GameObject cam;
public float sensitivity = 2f;
public float walk_speed = 2f;
public float run_speed = 2f;

CharacterController player_CC;
float speed;
float moveFB;
float moveLR;
float rotX;
float rotY;
bool canMove;


void Start () {

    canMove = true;

    player_CC = GetComponent<CharacterController>();

    speed = walk_speed;

}


void Update () {

    if (canMove)
    {
        moveFB = Input.GetAxis("Vertical") * speed;
        moveLR = Input.GetAxis("Horizontal") * speed;

        rotX = Input.GetAxis("Mouse X") * sensitivity;
        rotY = Input.GetAxis("Mouse Y") * sensitivity;

        Vector3 movement = new Vector3(moveLR, 0, moveFB);
        transform.Rotate(0, rotX, 0);
        cam.transform.Rotate(rotY, 0, 0);
        movement = transform.rotation * movement;
        player_CC.Move(movement * Time.deltaTime);
    }

    if (Input.GetKey(KeyCode.LeftShift))
    {
        speed = run_speed;
    } else
    {
        speed = walk_speed;
    }

}

Unity GameObject Picture

2

2 Answers

2
votes

movement = transform.rotation * movement;

You're multiplying your transforms rotation by your movement vector. Separate the logic.

0
votes

I know what caused it. But i dont know why it did it. But i used a charachtercontroller and a rigidbody on the same object. Sorry for wasting your time :/