Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

make FPS controller rotate at the same y-axis as the camera when input is held?

Discussion in 'Scripting' started by Treasureman, Feb 15, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a FPS controller set up using the mouse orbit script. my problem is i want my character controller to have to same y-axis (rotation) as the main camera when you hold the axis vertical or horizontal. I tried this guys script but it maid me character fly in the air.
    http://forum.unity3d.com/threads/dead-space-style-camera-help.171284/

    If you've ever played Dead Space then I just want a character that rotates like that.
     
  2. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    Doesn't mouse look do that?
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I want the camera to rotate around the player when he's standing still but have the character face away from the camera when he moves.
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Kinda makes no sense to do this in an FPS.
     
  5. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    You can detect when the player moves by storing the last vector position and the current vector position and getting the magnitude (i think itsVector3.Distance() in unity) then if its > 0.1 or what ever rotate the player if its nto then dont rotate the player.

    Code (JavaScript):
    1. var target : Transform;
    2. var distance = 10.0;
    3. var LastPos = new Vector3(0,0,0);
    4. var debug = 0.0;
    5. var xSpeed = 250.0;
    6. var ySpeed = 120.0;
    7.  
    8. var yMinLimit = -20;
    9. var yMaxLimit = 80;
    10.  
    11. private var x = 0.0;
    12. private var y = 0.0;
    13.  
    14. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    15.  
    16. function Start () {
    17.     var angles = transform.eulerAngles;
    18.     x = angles.y;
    19.     y = angles.x;
    20.  
    21.     // Make the rigid body not change rotation
    22.        if (GetComponent.<Rigidbody>())
    23.         GetComponent.<Rigidbody>().freezeRotation = true;
    24. }
    25.  
    26. function LateUpdate () {
    27.    var Magnitude = Vector3.Distance(target.position, LastPos);
    28.    debug = Magnitude;
    29.     if(Magnitude > 0.05)
    30.     {
    31.          target.localEulerAngles = new Vector3(target.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, target.localEulerAngles.z);
    32.     }
    33.     LastPos = target.position;
    34.     if (target) {
    35.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    36.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    37.        
    38.          y = ClampAngle(y, yMinLimit, yMaxLimit);
    39.                
    40.         var rotation = Quaternion.Euler(y, x, 0);
    41.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    42.        
    43.         transform.rotation = rotation;
    44.         transform.position = position;
    45.     }
    46. }
    47.  
    48. static function ClampAngle (angle : float, min : float, max : float) {
    49.     if (angle < -360)
    50.         angle += 360;
    51.     if (angle > 360)
    52.         angle -= 360;
    53.     return Mathf.Clamp (angle, min, max);
    54. }
    Thats just a proof of concept using mouseorbit it should work you will probably need to smooth it out make sure you use a main camera thats not a child of the player.
     
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I'm having trouble figuring out how to set it up. Can you tell me step by step how
     
  7. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    this one was just set up on a FPS character controller as a example method:

    Add a fps character to the scene delete its camera
    Add a MainCamera
    Attach script to camera
    Attach target to script

    You should be able to put this into a camera scritp you make but you need to make it smoother.

    Code (JavaScript):
    1.    var Magnitude = Vector3.Distance(target.position, LastPos);
    2.    debug = Magnitude;
    3.     if(Magnitude > 0.05)
    4.     {
    5.          target.localEulerAngles = new Vector3(target.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, target.localEulerAngles.z);
    6.     }
    7.     LastPos = target.position;
     
  8. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Thanks!!! I've been trying for months to figure this out. But one question, where would I put the input 'Aim' so it would do the same thing as the horizontal and vertical axis. The 'Aim' input is a joystick button.