Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Quick question.

Discussion in 'Scripting' started by AlphaWalker0, Aug 4, 2021.

  1. AlphaWalker0

    AlphaWalker0

    Joined:
    Jun 6, 2021
    Posts:
    16
    How do I make a 2D camera follow a sprite without making the camera rotate?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    After you move the sprite each frame, copy its position (with an appropriate offset) to the camera.
     
  3. AlphaWalker0

    AlphaWalker0

    Joined:
    Jun 6, 2021
    Posts:
    16
    Can you give the code to that?
     
  4. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    there is a great Unity Package called Cinemachine
    you can find it by opening the Package Manager (Window/PackageManager) in the Unity Editor and installing it there

    Cinemachine contains already premade scripts for cameras to follow an object, including some control parameters to define how it shall follow that object, like confinements, damping, dead zones etc:
     
  5. AlphaWalker0

    AlphaWalker0

    Joined:
    Jun 6, 2021
    Posts:
    16
    I'm not using a plugin like that. Way to complicated for me. I just want a simpler approach that just makes the camera follow the player without rotation.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Really?

    Code (csharp):
    1. MyCameraTransform.position = MySpriteTransform.position + DesiredCameraOffset;
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    Is cinemachine TPP camera take into account colliders like walls and other occluders ?
     
  8. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    not sure what TPP is, but yes it sorta has, there is an additional cinemachine Collider component, that if i remember correctly simply moves the camera forward closer to its look-at target up to a non-obstructed line of sight, if something gets in between the camera and the target

    though i have never really used that component, just tested it once

    it also has confinements where you pretty much define an area the camera (or rather the field of view of the camera) is not allowed to leave, which is super usefull when you have a limited map-size and want to assure that the camera does not see anything out of bounds (like a Top Down or Side scrolling perspective with clear borders) :)
     
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Assuming "third person perspective".