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

2D Game and Camera Folow script. How to follow the player?

Discussion in 'Scripting' started by Litos001, Mar 27, 2014.

  1. Litos001

    Litos001

    Joined:
    Jun 12, 2013
    Posts:
    13
    Hi, I've create this script attached to the camera to follow my character.

    Code (csharp):
    1.  
    2. public class CameraFollow : MonoBehaviour {
    3.  
    4.     private Transform player;       // Reference to the player's transform.
    5.  
    6.     void Awake ()
    7.     {
    8.         // Setting up the reference.
    9.         player = GameObject.FindGameObjectWithTag("Player").transform;
    10.     }
    11.    
    12.    
    13.     void Update ()
    14.     {
    15.         transform.position = new Vector3 (player.position.x + 2, player.position.y, -10);
    16.     }
    17. }
    18.  
    As you can see, the camera is 2 points ahead of my character, but what i want, is that when my players moves to the left, the camera moves to points in the opposite side.

    how can i do that??

    Thanks in advance.
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I am not sure exactly what you are after, but perhaps my little camera script will be helpful to you:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class CameraFollow2D : MonoBehaviour {
    5.    
    6.     // Transform of target object, defaults to player
    7.     public Transform target;
    8.     // Time in seconds for smoothing
    9.     public float smoothTime = 0.5f;
    10.     // Distance from target
    11.     public float distance = 5.0f;
    12.    
    13.     // Local copy of transform to reduce component lookups
    14.     Transform _transform;
    15.     // Velocity of camera smoothing
    16.     Vector3 _smoothVelocity;
    17.    
    18.     void Start() {
    19.         _transform = transform;
    20.        
    21.         if (target == null) {
    22.             // Point at player by default
    23.             GameObject player = GameObject.FindWithTag("Player");
    24.             if (player != null)
    25.                 target = player.transform;
    26.         }
    27.     }
    28.    
    29.     void Update() {
    30.         if (target != null) {
    31.             // Point camera towards target
    32.             Vector3 targetPosition = target.position;
    33.             targetPosition.z -= distance;
    34.            
    35.             _transform.position = Vector3.SmoothDamp(_transform.position, targetPosition, ref _smoothVelocity, smoothTime);
    36.         }
    37.     }
    38. }
    39.  
    If not, just ignore it :p hehe
     
  3. Litos001

    Litos001

    Joined:
    Jun 12, 2013
    Posts:
    13
    Thank you very much. That's was really helpfull !!! :D