user16835604
user16835604

Reputation:

Unity problem with creating my own fps controller

So I'm making a first person player controller for my game, but I have one small problem. My code works fine until I rotate 180 degrees with my camera, then the movement gets inverted.

Some variable declarations:

My code:

gameObject.transform.Translate((playerCamera.transform.right * horizontal).x * Time.deltaTime * speed, 
            0f, (playerCamera.transform.forward * vertical).z * Time.deltaTime * speed);

The first person camera works fine.

Edit: I switched to character controller and now it works.

Upvotes: 0

Views: 443

Answers (1)

rustyBucketBay
rustyBucketBay

Reputation: 4551

You need to specify what gameObject the script is attached to and the hierarchy of the gameobjects involved in the scene. Probably you are translating one gameObject based in others gameObject transform and they are not parented, so if they don't move along you dont get the outcome you want.

gameObject.transform.Translate((playerCamera.transform.right * horizontal).x * 
^^^^^^^^^^^^^^^^^^^^            ^^^^^^^^^^^^^^^^^^^^^^ 
Time.deltaTime * speed, 
            0f, (playerCamera.transform.forward * vertical).z * Time.deltaTime * speed);

Upvotes: 0

Related Questions