x


Keyboard arrows turn around

Hi I am using First Person Control, I disabled MouseLook, so I can only control the game by keyboard arrows.But the arrows can only go forward/backward/right/left, I cannot turn around. I want when I press the right arrow, I will turn 45 degrees or 90 degrees, then I can use the up arrow to move forward. How can I do this?

Thanks

more ▼

asked Nov 17 '10 at 02:52 AM

Lucy gravatar image

Lucy
32 8 8 15

What do you mean exactly by '45 or 90 degrees'? Do you want the character to turn at fixed angular intervals? (E.g. every time you press the left arrow key the character turns 45 degrees to the left.)

Nov 17 '10 at 05:41 AM Jesse Anders

Hi Jesse:) Sorry for the confusing. You know when you play a 3D game, you only use arrows to go straight or turn around, then use mouse to click. So I want to make my game like that, but the arrow doesn't do turnings, it only moves like parallel. It's like I walk toward left without turning my face to left...

Nov 17 '10 at 10:37 PM Lucy
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

In FPSInputController.js, the line

var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

controls your movement on the x-axis/sideways (horizontal input keys) and z-axis/in&out (vertical input keys). If you remove 'Input.GetAxis("Horizontal")' you'll no longer be able to strafe. You can add a line right after it to rotate you along the y-axis based on the horizontal input keys:

transform.Rotate(Vector3.up, Input.GetAxix("Horizontal") * rotationSpeed * Time.deltaTime);
more ▼

answered Nov 17 '10 at 07:17 AM

ryleigh gravatar image

ryleigh
29 1 1 5

Sorry I can't find FPSInputController.js, where should I put these code? Thanks!

Nov 17 '10 at 11:05 PM Lucy

To edit FPSInputController.js, select the First Person Controller in the scene and look at the Inspector. Click on the little blank page icon that says "FPSInputController (MonoScript)" beside it. This will highlight and focus the script in the Project pane (located in Standard Assets/Character Controllers/Sources/Scripts)

Nov 26 '10 at 09:02 AM ryleigh

Hello, Hopefully someone still looks at this thread. Ryleigh, I am trying to follow what you said. I got rid of the 'Input.GetAxis("Horizontal")' then inputed the other line but would get one error after another. Do you by chance know why? This is exactly what I'm looking for and have been stuck for ages!

Apr 15 '12 at 11:00 PM BluEye
(comments are locked)
10|3000 characters needed characters left

changed the first 2 if's so it should work better now:

function Update () {

if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, -1);

if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, 1);

if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);

if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);

}

as i said above use this in a new java script and put it on your camera

more ▼

answered Nov 21 '10 at 08:10 PM

Scribe gravatar image

Scribe
1.4k 12 16 34

I have the First Person Control, so I put it on the camera which inside the First Person Controller, is that right?

When I press the left/right arrow key, it's not turing, it still goes parellel.

Another question, how can I disable my mouse? I want to use arrow keys to go forward/backward or turn around, use mouse just for clicking buttons

Thank you

Nov 22 '10 at 12:15 AM Lucy

for me the script i put up works fine on the camera but i have not tried putting it in a 'first person controller'. As for the mouse are you asking to make it stop moving your character, in which case you can just find that part of the script and delete it. Or you can hide the mouse but if it is going to be used for click then this would be useless (i don't understand what you mean by disable)

Nov 22 '10 at 10:40 AM Scribe

Thanks for replying. At the moment, I can use mouse to point sky and point ground, the mouse can be pointing to anywhere, it makes me busy to use keyboard to go forward/backward, and use mouse to change directions

Nov 23 '10 at 03:55 AM Lucy

so if you want to stop moving the camera with the mouse you will have to find that part of the script and get rid of (sorry still not quite sure what your asking the mouse to do). P.S did the script i posted work in the end or are you still having problems (if your using the character control script it gives it might be that it is already using a code to move forward, backwards and side-to-side and this part of it might also need to be deleted)

Nov 23 '10 at 05:32 PM Scribe

sorry it doesn't work. In First Person Controller, I changed minimumY and maximumY to 0, now the mouse won't go up and down, but no idea of X

Thank you

Nov 25 '10 at 04:00 AM Lucy
(comments are locked)
10|3000 characters needed characters left

Hi Lucy and Scribe,

I have been reading your post an the indications from Scribe and finally got it work!

As I couldn't attach the file, below you'll find the complete code.

Regards,

German ==================================================================

private var motor : CharacterMotor;

// Use this for initialization
function Awake () {
    motor = GetComponent(CharacterMotor);
}

// Update is called once per frame
function Update () {
    // Get the input vector from kayboard or analog stick
    //var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); Línea Original
    var directionVector = new Vector3(0, Input.GetAxis("Vertical"));
    transform.Rotate(Vector3.up, Input.GetAxis("Horizontal") * 0 * Time.deltaTime); //Linea Adicional
    if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, -1* Time.deltaTime);//Lineas para otro script?
    if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, 1* Time.deltaTime);
    if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);
    if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);//Termina la Modificación



    if (directionVector != Vector3.zero) {
       // Get the length of the directon vector and then normalize it
       // Dividing by the length is cheaper than normalizing when we already have the length anyway
       var directionLength = directionVector.magnitude;
       directionVector = directionVector / directionLength;

       // Make sure the length is no bigger than 1
       directionLength = Mathf.Min(1, directionLength);

       // Make the input vector more sensitive towards the extremes and less sensitive in the middle
       // This makes it easier to control slow speeds when using analog sticks
       directionLength = directionLength * directionLength;

       // Multiply the normalized direction vector by the modified length
       directionVector = directionVector * directionLength;
    }

    // Apply the direction to the CharacterMotor
    motor.inputMoveDirection = transform.rotation * directionVector;
    motor.inputJump = Input.GetButton("Jump");
}

// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
more ▼

answered Oct 27 '12 at 08:26 AM

german2209 gravatar image

german2209
1

(comments are locked)
10|3000 characters needed characters left

function Update () {

if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(Vector3.forward * Time.deltaTime);

if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(-Vector3.forward * Time.deltaTime);

if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);

if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);

}

more ▼

answered Nov 20 '10 at 05:08 PM

Scribe gravatar image

Scribe
1.4k 12 16 34

where should I put these code? Do I create a new .js file and attach it into the main camera? Thanks

Nov 21 '10 at 07:47 PM Lucy

yep you make a new .js file and copy and paste this code. then put it on the camera it should work

Nov 21 '10 at 08:04 PM Scribe
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2166
x286
x195
x18
x1

asked: Nov 17 '10 at 02:52 AM

Seen: 6888 times

Last Updated: Oct 27 '12 at 08:26 AM