Search Unity

Wolfensteinish controller

Discussion in 'Getting Started' started by boolfone, Jun 22, 2016.

  1. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Does anyone know where I can get an all-keyboard first-person controller?

    I have a game I want in a web site, and it can get confusing/annoying with the mouse (http://coolfone.mobi/fpmaze6/).

    I basically want the right and left arrows to rotate and to have the mouse basically do nothing.

    Thanks.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Why not write it yourself? Just rotate by Input.GetAxis("Horizontal"), and translate forward by Input.GetAxis("Vertical") (each suitably scaled for speed and multiplied by Time.deltaTime, of course).
     
  3. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Thanks for the help.

    I wrote some code for the Wolfensteinish controls, and I put a new version up here:

    http://coolfone.mobi/fpmaze7/

    Here’s the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KBMove : MonoBehaviour
    5. {
    6.     public float moveSpeed = 5f;
    7.     public float rotateSpeed = 2f;
    8.     CharacterController _characterController;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         _characterController = GetComponent<CharacterController> ();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.  
    19.         float horiz = Input.GetAxis ("Horizontal");
    20.         this.transform.RotateAround (transform.position, Vector3.up, horiz * rotateSpeed);
    21.         float vert = Input.GetAxis ("Vertical");
    22.         _characterController.Move (transform.forward * Time.fixedDeltaTime * vert * moveSpeed);
    23.  
    24.     }
    25. }
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Pretty good. But, you shouldn't use Time.fixedDeltaTime in Update; use Time.deltaTime instead. And you should use it for the rotation as well, otherwise your rotation rate will depend on the frame rate, which is a bad thing.

    Otherwise, nice job!
     
  5. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I decided to put the rotation in Update() and the movement in FixedUpdate() since that’s what FirstPersonController.cs seems to do.

    Here’s what I have now:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KBMove : MonoBehaviour
    5. {
    6.     public float moveSpeed = 5f;
    7.     public float rotateSpeed = 100f;
    8.     CharacterController _characterController;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         _characterController = GetComponent<CharacterController> ();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.  
    19.         float horiz = Input.GetAxis ("Horizontal");
    20.         this.transform.RotateAround (transform.position, Vector3.up, horiz * rotateSpeed * Time.deltaTime);
    21.  
    22.  
    23.     }
    24.  
    25.     void FixedUpdate()
    26.     {
    27.         float vert = Input.GetAxis ("Vertical");
    28.         _characterController.Move (transform.forward * Time.fixedDeltaTime * vert * moveSpeed);
    29.  
    30.     }
    31.  
    32. }
    I put the new version up over here:

    http://coolfone.mobi/fpmaze8/

    Let me know if you have any thoughts on it.

    Thanks.
     
    Last edited: Jun 23, 2016
    JoeStrout likes this.