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

how to make the camera rotate around the player instead of..

Discussion in 'Scripting' started by eskovas, Jan 25, 2010.

  1. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    well im using a camera script (dont remember where i found it)

    heres the problem:


    with this script, the camera rotates around the offset i create... but i want it to rotate around the player

    the script:

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var targetOffset = Vector3.zero;
    4. var distance = 4.0;
    5.  
    6. var lineOfSightMask : LayerMask = 0;
    7. var closerRadius : float = 0.2;
    8. var closerSnapLag : float = 0.2;
    9.  
    10. var xSpeed = 200.0;
    11. var ySpeed = 80.0;
    12.  
    13. var yMinLimit = 0;
    14. var yMaxLimit = 80;
    15.  
    16. private var currentDistance = 10.0;
    17. private var x = 0.0;
    18. private var y = 0.0;
    19. private var distanceVelocity = 0.0;
    20.  
    21. function Start () {
    22.     var angles = transform.eulerAngles;
    23.     x = angles.y;
    24.     y = angles.x;
    25.     currentDistance = distance;
    26.    
    27.     // Make the rigid body not change rotation
    28.     if (rigidbody)
    29.         rigidbody.freezeRotation = true;
    30. }
    31.  
    32. function LateUpdate () {
    33.     if (target) {
    34.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    35.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    36.        
    37.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    38.                
    39.         var rotation = Quaternion.Euler(y, x , 0);
    40.         var targetPos = target.position + targetOffset;
    41.         var direction = rotation * -Vector3.forward;
    42.        
    43.         var targetDistance = AdjustLineOfSight(targetPos, direction);
    44.         currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, distanceVelocity, closerSnapLag * .3);
    45.        
    46.         transform.rotation = rotation;
    47.         transform.position = targetPos + direction * currentDistance ;
    48.        
    49.     }
    50. }
    51.  
    52. function AdjustLineOfSight (target : Vector3, direction : Vector3) : float
    53. {
    54.     var hit : RaycastHit;
    55.     if (Physics.Raycast (target, direction, hit, distance, lineOfSightMask.value))
    56.         return hit.distance - closerRadius;
    57.     else
    58.         return distance;
    59. }
    60.  
    61. static function ClampAngle (angle : float, min : float, max : float) {
    62.     if (angle < -360)
    63.         angle += 360;
    64.     if (angle > 360)
    65.         angle -= 360;
    66.     return Mathf.Clamp (angle, min, max);
    67. }
    68.  
    69.  
    ive tried so many things and none of those worked

    can someone help me with this?

    thanks
     
  2. phort99

    phort99

    Joined:
    Oct 20, 2009
    Posts:
    76
    The script looks okay to me.

    The first line of the script is var target:Transform. The script attached to your camera should then have a field you can set to the gameobject you want as your target. Is that set? The script is set up to use that target's transform but it can't if that value is null.

    Drag and drop your character from the Hierarchy window into the "Target" field in the Inspector window to set it to your character.
     
  3. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    the "problem" is that the camera rotates around the offset that i created... i want it to rotate around the player

    everything else is good

    EDIT:

    i want the camera to be facing the offset and to rotate around the player
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can use transform.LookAt on the camera's transform to make it point at a target object.