Search Unity

Rotating camera back to normal rotation - not working!

Discussion in 'Scripting' started by xamur, Jun 29, 2017.

  1. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Hello folks,
    to make it short:

    I have a camera attached to a gameObject called player. I want to change the local z-Rotation of the camera while the player is sliding on the ground (see the script below). It does rotate it while he is sliding, but it does not rotate it back after sliding is false.

    Script (Slide method):
    Code (CSharp):
    1. void Slide()
    2.     {
    3.         if (Input.GetKeyDown (crouchKey) && isGrounded && running && !sliding && !changingState)
    4.         {
    5.             sliding = true;
    6.             running = false;
    7.             crouching = false;
    8.             proning = false;
    9.  
    10.             slideTimer = 0.0f;
    11.         }
    12.  
    13.         if (sliding)
    14.         {
    15.             StartCoroutine (GoToState (slide));
    16.             currentSpeed = slideSpeed;
    17.  
    18.             // We set the FOV back to normal
    19.             mainCamera.fieldOfView = normalFOV;
    20.  
    21.             mainCamera.transform.localEulerAngles = Vector3.Lerp (currentCameraRotation, cameraSlideRotation, Time.deltaTime * 5);
    22.  
    23.             // The speed while 'sliding' doesn't get affected by others
    24.             xDirection = Vector3.zero;
    25.             yDirection = Vector3.zero;
    26.  
    27.             direction = mainCamera.transform.forward;
    28.             direction *= currentSpeed;
    29.  
    30.             charController.Move (direction * Time.deltaTime);
    31.  
    32.  
    33.             slideTimer += Time.deltaTime;
    34.  
    35.             if (slideTimer >= maxSlideTimer)
    36.             {
    37.                 slideTimer = 0.0f;
    38.                 StartCoroutine (GoToState (crouch));
    39.                 crouching = true;
    40.                 mainCamera.transform.localEulerAngles = Vector3.Lerp (currentCameraRotation, normalCameraRotation, Time.deltaTime * 5);
    41.                 sliding = false;
    42.             }
    43.         }
    44.     }
    I am updating the 'currentCameraRotation' everytime in Update.

    Thank you!
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    I don't really understand this script. Is this Slide() function called every frame? It looks like it's serving as both the Slide() function and an update function?

    If it's called every frame, what is StartCoroutine(GoToState(slide)) doing? Is this meant to be called every frame?

    Secondly, I think you'd be better off ditching euler angles, and use Quaternion.RotateTowards instead.

    Finally, you'repretty much using the same value when lerping. If currentCameraRotation doesn't actually change, then the lerp is stuck giving you the same value every frame.
     
  3. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Slide() gets called in Run() and Run gets called in a function called Move(), and Move gets called in Update().

    StartCoroutine(GoToState(slide)) is not important here. It just changes the the height and radius of the Character Controller for each state. And affects the camera position only.

    Well, I thought when I am updating the camera rotation every frame, it does change, because I am changing it in Slide(). I mean, if currentCameraRotation is (0,0,0) at the beginning, changes to (0,0,25) in Slide(), then currentCameraRotation should be (0,0,25), or am I wrong?
     
  4. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Bump