1
\$\begingroup\$

So, I made a simple First Person Camera script for my game, and the way it's supposed to work is the camera is rotated by localEulerAngles to rotate the camera around, and then just the Y-axis rotation is also applied to the character, so that he moves in the direction of the camera.

This isn't how its working now.

I've narrowed down the two most likely problems in the scripts, and this first one is from the camera controller:

player.transform.localEulerAngles = new Vector3(0, rotationX, 0); cam.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);

Note: the camera works just fine, and rotates perfectly in all directions.

Secondly, the player movement controller, which might not be actually be moving locally? I can't tell.

transform.Translate (new Vector3(0, 0, Input.GetAxis("Vertical") * walkSpeed)); transform.Translate (new Vector3(Input.GetAxis("Horizontal") * strafeSpeed, 0, 0));

There you go. Right now, even though the camera moves just fine with the mouse, and, incidentally, the whole script used to work, the player moves forward on the global Z-axis with W and backwards with S, all the while acting like a shooter on rails.

Thank you!

\$\endgroup\$
1
\$\begingroup\$

Currently you simply add transformation based on your input value. The axis are locked. So no matter which rotation your gameobject has, it always will transform into the locked in axis based on your input.

You need to change the forward direction based on the current forward direction of your camera.

In this case, use Camera.main and get the forward postion of it and apply it to your translate.

Heres an example:

// Update is called once per frame

void Update () {

     Vector3 MouseWorldPosition = MainCamera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
     transform.LookAt(MouseWorldPosition);
     transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));
     // Amout to move
     float inputX = Input.GetAxis("Horizontal") * PlayerSpeed *

     Time.deltaTime;
     float inputY = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;
     // Move the Player
     transform.Translate(Vector3.forward * inputX) ;
     transform.Translate(Vector3.left * inputY) ;
     if (Input.GetKey("space"))
     {
         //Fire projectile
         Instantiate(ProjectilePrefab, transform.position,Quaternion.identity);
     }
 }
\$\endgroup\$
  • \$\begingroup\$ Could you add some commenting on that code? I'm not quite understanding where the camera look function is. \$\endgroup\$ – Isy S Jun 8 '18 at 9:41
  • \$\begingroup\$ The first line in the code creates a vector 3 based on the main camera forward direction. Than you simply apply this to your component via LookAt, which will force your object to look at the same direction your camera does (which is controlled by your mouse input). At the bottom you simply add relative the forward, backward, left, right attributes. \$\endgroup\$ – Prometheus Jun 8 '18 at 13:44

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.