Search Unity

[Resolved] problem with Script for Rotate Camera and player

Discussion in 'Navigation' started by Teilor111, May 22, 2016.

  1. Teilor111

    Teilor111

    Joined:
    May 22, 2016
    Posts:
    7
    Hi Dears,


    I found following script for move my camera in game.
    Main Character (player) rotates according to the rotation of the camera.
    But when I press "W" or "Up Arrow" and character move forward, camera is locked and i can move only in up/down, not in right/left.
    Could you tell me why? I trying modify this script but I cant find this moment which block it.
    Thanks for any help


    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         Vector3 Angles = transform.eulerAngles;
    5.         x = Angles.x;
    6.         y = Angles.y;
    7.         currentDistance = distance;
    8.         desireDistance = distance;
    9.         correctedDistance = distance;
    10.     }
    11.  
    12.     void LateUpdate()
    13.     {
    14.         x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
    15.         y += Input.GetAxis("Mouse Y") * mouseYSpeedMod;
    16.  
    17.         if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    18.         {
    19.             float targetRotantionAngle = CameraTarget.eulerAngles.y;
    20.             float cameraRotationAngle = transform.eulerAngles.y;
    21.             x = Mathf.LerpAngle(cameraRotationAngle, targetRotantionAngle, lerpRate * Time.deltaTime);
    22.         }
    23.  
    24.         y = ClampAngle(y, -15, 25);
    25.         Quaternion rotation = Quaternion.Euler(y, x, 0);
    26.  
    27.         desireDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ZoomRate * Mathf.Abs(desireDistance);
    28.         desireDistance = Mathf.Clamp(desireDistance, MinViewDistance, MaxViewDistance);
    29.         correctedDistance = desireDistance;
    30.  
    31.         Vector3 position = CameraTarget.position - (rotation * Vector3.forward * desireDistance);
    32.  
    33.         RaycastHit collisionHit;
    34.         Vector3 cameraTargetPosition = new Vector3(CameraTarget.position.x, CameraTarget.position.y + cameraTargetHeight, CameraTarget.position.z);
    35.  
    36.         bool isCorrected = false;
    37.    
    38.         if (Physics.Linecast(cameraTargetPosition, position, out collisionHit))
    39.         {
    40.             position = collisionHit.point;
    41.             correctedDistance = Vector3.Distance(cameraTargetPosition, position);
    42.             isCorrected = true;
    43.         }
    44.  
    45.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * ZoomRate) : correctedDistance;
    46.  
    47.  
    48.         position = CameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -cameraTargetHeight, 0));
    49.  
    50.         transform.rotation = rotation;
    51.         transform.position = position;
    52.  
    53.         float cameraX = transform.rotation.x;
    54.         CameraTarget.eulerAngles = new Vector3(cameraX, transform.eulerAngles.y, transform.eulerAngles.z);
    55.     }
    56.  
    57.  
    58.  
     
    Last edited: May 24, 2016
  2. Teilor111

    Teilor111

    Joined:
    May 22, 2016
    Posts:
    7
    RESOLVED:

    Problem fixed by comment following lines:

    //if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    //{
    // float targetRotantionAngle = CameraTarget.eulerAngles.y;
    // float cameraRotationAngle = transform.eulerAngles.y;
    // x = Mathf.LerpAngle(cameraRotationAngle, targetRotantionAngle, lerpRate * Time.deltaTime);
    //}

    Post for close.