Search Unity

Keeping the camera above the ground

Discussion in 'Scripting' started by BFDesign, Apr 4, 2011.

  1. BFDesign

    BFDesign

    Joined:
    Mar 16, 2011
    Posts:
    214
    Howdy All,

    Just a quick question. Anybody have any suggestions on keeping the third person follow camera from going "underground" when moving around the terrain? I'd appreciate ANY help at all.

    Thank you,
    --Richard
     
  2. CheyTac

    CheyTac

    Joined:
    Feb 17, 2011
    Posts:
    71
    if(transform.position.y <= 0)
    transform.position.y = 0;
     
  3. BFDesign

    BFDesign

    Joined:
    Mar 16, 2011
    Posts:
    214
    Thanks for the post,

    I plugged that bit into the script but it doesn't seem to be having any effect. I'm not getting any errors either. Any thoughts?

    By the way... I love that picture.
     
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    the previous example assumes you have flat ground everywhere for y = 0.

    To do what he suggested you really need to do a raycast from above on your terrain then use that y value. You might need to add a small amount to it to ensure it is above the terrain.
     
  5. BFDesign

    BFDesign

    Joined:
    Mar 16, 2011
    Posts:
    214
    Thanks a lot Raider!
     
  6. BFDesign

    BFDesign

    Joined:
    Mar 16, 2011
    Posts:
    214
    How exactly would I go about accomplishing that raycast task?
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Expert from a previous post

    Code (csharp):
    1.  
    2.             // raycast test for distance
    3.             dist=distance;
    4.             var hit : RaycastHit;
    5.             if(Physics.Raycast(camera.position, currentRotation * -Vector3.forward, hit, distance)){
    6.                 dist=(hit.point-camera.position).magnitude - 1;
    7.                
    8.             }
    9.  
    Look into that code for a better explanation of what it all does, but that is the code that does a raycast from the target (focalpoint) to where the camera should be and gives the distance to that point. (minus 1 unit to prevent clipping)

    http://forum.unity3d.com/threads/76973-Car-Veiw-script-problem?p=493081&viewfull=1#post493081
     
  8. BFDesign

    BFDesign

    Joined:
    Mar 16, 2011
    Posts:
    214
    Thanks, BigMisterB