Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

First Person Controller

Discussion in 'Editor & General Support' started by empedocles, Dec 2, 2006.

  1. empedocles

    empedocles

    Joined:
    Dec 2, 2006
    Posts:
    3
    Some of our students have been exploring a variety of game construction packages for use in our university digital lab. Our needs are modest and we have been excited about the potential of Unity to support some areas of our curriculum.

    One very basic question has arisen: How can the First Person Controller be modified to allow rotary movement (instead of "strafing movement) to occur through the use of the arrow keys, i.e., right arrow key rotates viewpoint right, left arrow key rotates viewpoint left.

    I'm sure some of our students could discover this with a bit of effort, but any help would greatly expedite our current explorations.
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Look at the sample move script in the character animation example project. Swap it with the FPS Walker script on the fps prefab.

    Or swap transform.translate to transform.rotate in the current fps script
    on the correct line of course
    AC
     
  3. empedocles

    empedocles

    Joined:
    Dec 2, 2006
    Posts:
    3
    Thanks for the reply, Targos. I'm new in this environment, so appreciate all the assistance I can receive.

    I replaced the default FPS Walker script with the one found in the character animation project, but to no avail. The controls still act the same.

    I also examined the current fps script but could find no mention of "transform.translate" in order to make the swap you suggest.

    Perhaps this is all the result of my own inexperience, so I'll re-examine your instructions and see if anything new results.

    Thanks
     
  4. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    These are the lines in the SImpleMove script that rotate the character
    Code (csharp):
    1.  
    2. var newRotation = Input.GetAxis("Horizontal") * rotatationSpeed;
    3. transform.Rotate(0, newRotation * Time.deltaTime, 0);
    4.  
    In the FPSWalker script you will want to replace
    Code (csharp):
    1.  
    2. // This is creating a move vector based on the Horizontal
    3. //    and Vertical inputs. The vector is the results of the amount
    4. //    that the character should move on the Horizontal axis and
    5. //    the amount the character should move on the Vertical axis.
    6. //    That vector is then converted from
    7. //    local space of the character to world space so that the
    8. //    movement occurs in the proper direction.
    9. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    10. moveDirection = transform.TransformDirection(moveDirection);
    11.  
    with something like
    Code (csharp):
    1.  
    2. // Rotate your character using the Horizontal input
    3. var newRotation = Input.GetAxis("Horizontal") * rotatationSpeed;
    4. transform.Rotate(0, newRotation * Time.deltaTime, 0);
    5.  
    6. // Since the rotation is already done you now need to set the forward
    7. //    Vector based on the input and then convert to world space so the
    8. //    movement is properly done. The Horizonal input is not used because
    9. //    it was used to rotate the character instead of moving the character along
    10. //    the Horizontal axis.
    11. var newSpeed = Input.GetAxis("Vertical") * speed;
    12. var forward = transform.TransformDirection(Vector3.forward * newSpeed);
    13.  
    I have not tested this so you may need to play with it some.
     
  5. empedocles

    empedocles

    Joined:
    Dec 2, 2006
    Posts:
    3
    I appreciate the detailed advice, lfrog. I will review your instructions and see how they work out.