Search Unity

FPS input controller

Discussion in 'Scripting' started by SweetRevenge, Oct 13, 2010.

  1. SweetRevenge

    SweetRevenge

    Joined:
    Aug 28, 2010
    Posts:
    26
    Hey i just updated to Unity 3 and for some reason none of my Controller scripts seem to work. So im just going to use the First Person controller that comes with unity. I want to make a change to it however, I want to make it so that when you press s,d or the right or left arrow, he rotates in those directions, instead of moving in that direction. Can anyone help please?
    Code (csharp):
    1. private var motor : CharacterMotor;
    2.  
    3. // Use this for initialization
    4. function Awake () {
    5.     motor = GetComponent(CharacterMotor);
    6. }
    7.  
    8. // Update is called once per frame
    9. function Update () {
    10.     // Get the input vector from kayboard or analog stick
    11.     var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));    
    12.    
    13.  
    14.    
    15.     if (directionVector != Vector3.zero) {
    16.         // Get the length of the directon vector and then normalize it
    17.         // Dividing by the length is cheaper than normalizing when we already have the length anyway
    18.         var directionLength = directionVector.magnitude;
    19.         directionVector = directionVector / directionLength;
    20.        
    21.         // Make sure the length is no bigger than 1
    22.         directionLength = Mathf.Min(1, directionLength);
    23.        
    24.         // Make the input vector more sensitive towards the extremes and less sensitive in the middle
    25.         // This makes it easier to control slow speeds when using analog sticks
    26.         directionLength = directionLength * directionLength;
    27.        
    28.         // Multiply the normalized direction vector by the modified length
    29.         directionVector = directionVector * directionLength;
    30.    
    31.     }
    32.    
    33.     // Apply the direction to the CharacterMotor
    34.     motor.inputMoveDirection = transform.rotation * directionVector;
    35.     motor.inputJump = Input.GetButton("Jump");
    36. }
    37.  
    38. // Require a character controller to be attached to the same game object
    39. @script RequireComponent (CharacterMotor)
    40. @script AddComponentMenu ("Character/FPS Input Controller")
    41.  
     
  2. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    When you add a First Person Controller, you actually add a:

    Camera
    Capsule
    Character Motor script
    Mouse Look script
    FPS Input Controller

    The FPS Input Controller controls left/right/forward/backwards/jump movement, so if you don't want that, simply remove or disable the script. The Mouse Look script controls rotation, so that's the one you'll be interested in.

    For a first stab, you could try simply re-mapping MouseX and MouseY to the keyboard via Eidt | Project Settings | Input. If that doesn't work or doesn't work well enough, check out the Input class:

    http://unity3d.com/support/documentation/ScriptReference/Input.html

    The best method to use is probably via GetButton(), as this allows players to remap their controls as in most games. Click on Edit | Project Settings | Input | Axes to get the logical names of the various buttons.

    If that's too intimidating and re-mapping isn't necessary, GetKeyUp() and GetKeyDown() are available and those use KeyCodes which are quite straightforward. Eg, KeyCode.A = pressing the A key, KeyCode.B = pressing the B key, etc. If that's too hard, well, I can do custom programming for a fee. :)

    Good luck!
     
    Last edited: Oct 13, 2010
  3. Irishsun530

    Irishsun530

    Joined:
    Jun 23, 2012
    Posts:
    13
    @SweetRevenge did you ever figure this out?
     
  4. bilel

    bilel

    Joined:
    Apr 9, 2013
    Posts:
    1
    if you wanna get a rotation
    i suggest to change MouseLook script.
     
  5. Deepschalappatta

    Deepschalappatta

    Joined:
    Jul 27, 2013
    Posts:
    8
    You can edit "Get the input vector from kayboard or analog stick" (update function) from FPSInput Script and then add var directionVector = new Vector3(0, 0, Input.GetAxis("Vertical"));



    For Rotation You can add new script or add mouse lookup

    using UnityEngine;
    using System.Collections;

    public class PlayerMove : MonoBehaviour {

    public float turnSpeed=50f;

    void Update(){

    if(Input.GetKey(KeyCode.LeftArrow))
    transform.Rotate (Vector3.up,-turnSpeed*Time.deltaTime);

    if(Input.GetKey(KeyCode.RightArrow))
    transform.Rotate (Vector3.up,turnSpeed*Time.deltaTime);

    }
    }