Search Unity

Real camera Smooth Follow

Discussion in 'Scripting' started by Rakdos, Sep 27, 2009.

  1. Rakdos

    Rakdos

    Joined:
    Jun 4, 2009
    Posts:
    34
    Hello there !

    i have a problem with my smooth-following camera:

    i play a crate that is in third person
    a camera is following it with the script Smooth Follow
    Crate can rotate using mouse

    but here is my problem : how can i make that the camera cant go behind walls ? it doesn't work with collider :roll:
     
  2. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    Look this Topic about Wow Camera,
    http://forum.unity3d.com/viewtopic.php?t=18073

    the result that I liked most was:
    WowCamera.cs
    Posted for Paintbrush: Thu May 07, 2009 10:05 am


    but I have a problem in it.
    Can somebody help me in my error?

    Please enter in link to see my result of this script.
    http://www.universalgamesonline.com.br/unity3d/studies/wowcamera.cs.html

    Code (csharp):
    1.  
    2. //WowCamera.cs
    3.  
    4. //Credits to this script: "Paintbrush"
    5.  
    6.  
    7. using UnityEngine;
    8. using System.Collections;
    9.  
    10. public class WowCamera : MonoBehaviour
    11. {
    12.  
    13.     public Transform target;
    14.    
    15.     public float targetHeight = 1.7f;
    16.     public float distance = 5.0f;
    17.  
    18.     public float maxDistance = 20;
    19.     public float minDistance = .6f;
    20.  
    21.     public float xSpeed = 250.0f;
    22.     public float ySpeed = 120.0f;
    23.  
    24.     public int yMinLimit = -80;
    25.     public int yMaxLimit = 80;
    26.  
    27.     public int zoomRate = 40;
    28.  
    29.     public float rotationDampening = 3.0f;
    30.     public float zoomDampening = 5.0f;
    31.  
    32.     private float x = 0.0f;
    33.     private float y = 0.0f;
    34.     private float currentDistance;
    35.     private float desiredDistance;
    36.     private float correctedDistance;
    37.  
    38.    void Start ()
    39.     {
    40.         Vector3 angles = transform.eulerAngles;
    41.         x = angles.x;
    42.         y = angles.y;
    43.  
    44.         currentDistance = distance;
    45.         desiredDistance = distance;
    46.         correctedDistance = distance;
    47.  
    48.         // Make the rigid body not change rotation
    49.         if (rigidbody)
    50.             rigidbody.freezeRotation = true;
    51.    }
    52.    
    53.     /**
    54.      * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    55.      */
    56.    void LateUpdate ()
    57.     {
    58.        // Don't do anything if target is not defined
    59.         if (!target)
    60.             return;
    61.  
    62.         // If either mouse buttons are down, let the mouse govern camera position
    63.         if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
    64.         {
    65.             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    66.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    67.         }
    68.         // otherwise, ease behind the target if any of the directional keys are pressed
    69.         else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    70.         {
    71.             float targetRotationAngle = target.eulerAngles.y;
    72.             float currentRotationAngle = transform.eulerAngles.y;
    73.             x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    74.         }
    75.  
    76.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    77.  
    78.         // set camera rotation
    79.         Quaternion rotation = Quaternion.Euler(y, x, 0);
    80.  
    81.         // calculate the desired distance
    82.         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
    83.         desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
    84.         correctedDistance = desiredDistance;
    85.  
    86.         // calculate desired camera position
    87.         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));
    88.  
    89.         // check for collision using the true target's desired registration point as set by user using height
    90.         RaycastHit collisionHit;
    91.         Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);
    92.  
    93.         // if there was a collision, correct the camera position and calculate the corrected distance
    94.         bool isCorrected = false;
    95.         if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
    96.         {
    97.             position = collisionHit.point;
    98.             correctedDistance = Vector3.Distance(trueTargetPosition, position);
    99.             isCorrected = true;
    100.         }
    101.        
    102.         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
    103.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
    104.  
    105.         // recalculate position based on the new currentDistance
    106.         position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0));
    107.  
    108.         transform.rotation = rotation;
    109.         transform.position = position;
    110.    }
    111.  
    112.     private static float ClampAngle(float angle, float min, float max)
    113.     {
    114.         if (angle < -360)
    115.             angle += 360;
    116.         if (angle > 360)
    117.             angle -= 360;
    118.         return Mathf.Clamp(angle, min, max);
    119.     }
    120. }
    121.  
    122.  
     
  3. Rakdos

    Rakdos

    Joined:
    Jun 4, 2009
    Posts:
    34
    the js and cs script doesn't work very good :p
     
  4. vogles

    vogles

    Joined:
    Sep 28, 2009
    Posts:
    131
    I like the way the trees straighten when you get near them. its like they're slouching and then stand at attention when they notice you approaching. :eek:
     
  5. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    I dont understand.
    you say that i dont can use Javascript and C# in the same Project ?

    This is the Movimet Default of Tree in Unity. :p
     
  6. Rakdos

    Rakdos

    Joined:
    Jun 4, 2009
    Posts:
    34
    Of course, you didn't know that ? :)
     
  7. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    o_O
    i didnt know :eek:
    humm, then the cause of my error is this.

    i will try convert all my scripts to C# and test.

    thanks friend.

    please, if you have one camera Script same Wow, Perfect World, and if can share, i want ^^
     
  8. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    lol, he was kidding (see the smiley face?) You can absolutely use Javascript and C# in the same project, no problem.
     
  9. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    :?
    i am confuse now.

    anyway i am studying the new tutorial released in unitytutorials.com:
    EVAC-CITY Tutorial
    is amazing, i am learning very much ^^
    http://www.unitytutorials.com/document/298/how-to-make-a-game-similar-to-evac-city-in-unity3d

    but i somebody have any Camera same wow, perfect world, please share to we ^^
     
  10. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    No !!! You can use c# and java script togetheer. Obviously...
     
    Last edited: Aug 15, 2011
  11. frankrs

    frankrs

    Joined:
    Aug 29, 2009
    Posts:
    300
    not as well really, since one has to be compiled before the other if using get component. but thats easy to do if you put the first in the standard asset folder