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

Camera script problem

Discussion in 'Scripting' started by luciano182, Jan 29, 2015.

  1. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
    Hi people! I got a cool script that follows the player avoiding the "camera obstacles", but when my player falls I have problems.

    Please check my video.

    http://unity.lucianorasente.com/tmp/Cam/index.html

    I want to switch to another camera script when the player is falling.

    Do you know a camera script useful for this? Or a magic asset?

    Thanks :)
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Which script are you using? Is it publicly available?

    In the video is seems that your character is below the ground as well. In the case that the look at position for the camera is also below the floor, this can easily lead to issues.
     
  3. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
    Yes is public, I'm using a customized version of "(MMO)RPG Camera & Controller".

    So do you think that my player is going bellow the floor? I don't know why happens. When the player is falling I disable the "Character Controller" and enable a "Box Collider".

    Please see my attachments.
     

    Attached Files:

  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  5. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
    My solution, looks very nice

    Code (CSharp):
    1.     Vector3 selectedPos;
    2.  
    3.     void Update() {
    4.         // ...
    5.         if(isFalling) {
    6.             CameraFalling();
    7.         }
    8.         else {
    9.              // ...
    10.         }
    11.  
    12.     }
    13.  
    14.     void ChoosePosFalling() {
    15.  
    16.         float deltaSide = 2;
    17.      
    18.         Vector3 [] positions = new Vector3[] {
    19.          
    20.             new Vector3(
    21.                 cameraPivot.transform.position.x + deltaSide,
    22.                 cameraPivot.transform.position.y + 1,
    23.                 cameraPivot.transform.position.z
    24.                 ),
    25.          
    26.             new Vector3(
    27.                 cameraPivot.transform.position.x - deltaSide,
    28.                 cameraPivot.transform.position.y + 1,
    29.                 cameraPivot.transform.position.z
    30.                 ),
    31.          
    32.             new Vector3(
    33.                 cameraPivot.transform.position.x,
    34.                 cameraPivot.transform.position.y + deltaSide,
    35.                 cameraPivot.transform.position.z + 1
    36.                 ),
    37.          
    38.             new Vector3(
    39.                 cameraPivot.transform.position.x,
    40.                 cameraPivot.transform.position.y + deltaSide,
    41.                 cameraPivot.transform.position.z - 1
    42.                 )
    43.         };
    44.      
    45.         Vector3 pos = positions[0];
    46.         float dist = 0;
    47.  
    48.         for(int i = 0; i < positions.Length; i++) {
    49.          
    50.             RaycastHit hit;
    51.             if (Physics.Raycast(positions[i], -Vector3.up, out hit)) {
    52.              
    53.                 if(hit.distance > dist) {
    54.                     pos = positions[i];
    55.                     dist = hit.distance;
    56.                 }
    57.              
    58.             }
    59.          
    60.         }
    61.  
    62.         selectedPos = pos;
    63.     }
    64.  
    65.     void CameraFalling() {
    66.  
    67.         transform.LookAt(cameraPivot.transform.position);
    68.  
    69.         ChoosePosFalling();
    70.  
    71.         //selectedPos.y = cameraPivot.transform.position.y + 1;
    72.         transform.position = selectedPos; // TODO: Smooth move to "selectedPos"
    73.  
    74.     }
    75.  
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Does it work now?
     
  7. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Good for you!
     
    luciano182 likes this.
  9. luciano182

    luciano182

    Joined:
    Oct 26, 2012
    Posts:
    28
    Thank you very much for your replies Dantus