0
\$\begingroup\$

I want my first person controller to turn left and right instead of just moving straight on that axis.

i have a 3d environment setup in "unity 5" and i am working with the "first person controller" script in unity 5 by default it moves my camera forward,back,left and right what i want is to change the left and right so that it turns the camera instead of moving straight over.

\$\endgroup\$
  • \$\begingroup\$ i am using unity 5 and i was referring to the first person controller script that comes with it. \$\endgroup\$ – sketcherskt Feb 3 '16 at 15:19
  • \$\begingroup\$ Please edit your question, it is very unclear. \$\endgroup\$ – Vaillancourt Feb 3 '16 at 15:20
  • \$\begingroup\$ i have a 3d environment setup and i am working with the "first person controller" script in unity 5 by default it moves my camera forward,back,left and right what i want is to change the left and right so that it turns the camera instead of moving straight over. \$\endgroup\$ – sketcherskt Feb 3 '16 at 15:31
  • \$\begingroup\$ Please edit your question, users tend to read that only and not the comments :) \$\endgroup\$ – Vaillancourt Feb 3 '16 at 15:32
1
\$\begingroup\$

What you want, is a rotation around the local y-axis when either A or D is pressed.

With the standard assets, this can be achieved quite easily, with the Input manager. To alter the settings, go to Edit->Project Settings->Input.
You should now see the settings for the Input in your Inspector window.

You want to change the Horizontal movement, so expand that and delete the Negative and Positive Buttons, as well as the alt buttons. Alternatively, you can set your preffered keys for horizontal movement. For Q and E, it would look like this:
Horizontal Input

If you look through the Settings, you can see a "Mouse X" setting. If you want to remove rotation with the mouse, you can directly edit this, or increment the Size field by 1. This will add another property at the end of the list, which you can edit.

Here are the important changes that need to be done, in order to work with the standard assets:

  • Name needs to be set to "Mouse X"
  • Negative and Positive Buttons need to be set, A and D in your case
  • Type has to be Key or Mouse Button

Also, you will probably want to set a high Gravity, so that the rotation stops quite soon, after releasing a key. You can also play around with the sensitivity, to get a faster or slower rotation.

The final input could look similar to this: Mouse X Input

\$\endgroup\$
  • \$\begingroup\$ This change doesn't affect the FPSInputcontroller. The rotational change is actually handled by MouseLook. So "viewer beware" as to which black box is doing what. \$\endgroup\$ – Draco18s no longer trusts SE Feb 3 '16 at 16:18
1
\$\begingroup\$

Open up the FPSInputController and CharacterMotor classes.

Look at how FPSInputController reads keypresses and then sends a command to the CharacterMotor. You will want to create two new commands to pass (or possibly more, I've added "crouch" and "sprint" to mine, you're looking for "rotate right" and "rotate left"). You'll have to define new inputs as well, Edit -> Project -> Input. Add 2 to the array size and define your keys.

Start with the FPSInputController as it will be simpler. Do not worry about if the lines you add throw errors that prevent compilation: you're simply trying to use a method you haven't made yet.

Next, go into the CharacterMotor and implement the methods you are attempting to call in the FPSInputcontroller. They might be named setRotateRight() and setRotateLeft(), so find an existing one, e.g. setJumping(), duplicate, rename, and alter the variable, then go define the variable (there are multiple classes inside this file, your input booleans should be part of the CharacterMotor class, not the internal subclasses).

Next, find the Update() function, it will call another function, go to its definition. Now add some lines that look at your rotateRight / rotateLeft booleans and do:

var dir:int = 0;
if(rotateRight && rotateLeft) dir = 0;
else if(rotateRight) dir = 1;
elseif(rotateLeft) dir = -1;

gameObject.transform.RotateAround(Vector3.zero, Vector3.up, dir * 20 * Time.deltaTime);

Alter the "20" amount until you have the desired rotation speed.

\$\endgroup\$

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.