Skip to content

Instantly share code, notes, and snippets.

@Fortyseven
Last active August 29, 2015 14:08
Show Gist options
  • Save Fortyseven/3edc9c0436ca341f6235 to your computer and use it in GitHub Desktop.
Save Fortyseven/3edc9c0436ca341f6235 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Camera_Follow_Offest_Flip_Lerpable2DScript : MonoBehaviour
{
//Set the amount of horizontal offset in the inspector
public Vector2 offset;
//Allow us to click and drag an object (that we want to follow) in the inspector
public Transform myObjectToFollow;
private Vector3 tempFollowedObjectWithOffset;
//Character flip
public DetectCharacterFlip2DScript characterFlipDetector;
//Lerp
public bool lerpCameraMovementOnX;
public float lerpSpeed;
void FixedUpdate ()
{
if (characterFlipDetector.facingRight) {
tempFollowedObjectWithOffset = new Vector3(myObjectToFollow.position.x + offset.x, myObjectToFollow.position.y + offset.y , transform.position.z);
}
else {
tempFollowedObjectWithOffset = new Vector3(myObjectToFollow.position.x - offset.x, myObjectToFollow.position.y + offset.y , transform.position.z);
}
if (lerpCameraMovementOnX){
transform.position = Vector3.Lerp(transform.position, tempFollowedObjectWithOffset, lerpSpeed ** Time.deltaTime);
}
else {
transform.position = tempFollowedObjectWithOffset;
}
}
}
using UnityEngine;
using System.Collections;
public class DetectCharacterFlip2DScript : MonoBehaviour
{
public bool facingRight = true;
void Update ()
{
if(Input.GetAxis("Horizontal") > 0 && !facingRight) {
facingRight = true;
transform.rotation = Quaternion.Euler(0,0,0);
}
else if(Input.GetAxis("Horizontal") < 0 && facingRight) {
facingRight = false;
transform.rotation = Quaternion.Euler(0,180,0);
}
}
}
@Fortyseven
Copy link
Author

From the original post on Reddit:

These two scripts allow you to create a camera follow script that not only has a lerping camera (a camera that slightly falls behind the player and then "catches up"), but also includes an X offset value. This means that depending on the direction the player is facing, the camera will slightly look ahead, and will automatically "flip" and look ahead in the proper direction when the player turns around.

The first script is a simple way to detect the direction the player is facing. Attach this script to your player.

"Input.GetAxis" is fueled by the negative/positive keys you setup in your Edit > Project Settings > Input Manager for a button. For example, by default on a new project, the "Horizontal" button is created for you, and A/D or Left/Right are setup as the default negative/positive keys respectively.

When the player presses the button "negatively" ("A" or "Left" on the keyboard), the value of "Horizontal" axis ramps up to -1. If pressed positively, it ramps up to (positive) 1.

public bool facingRight = true : By default, a boolean is "Null". Since most players are generally right-facing at start, we set facingRight to True so as to no confuse the script later on.

if(Input.GetAxis("Horizontal") > 0 && !facingRight) : If our player presses the "Horizontal" button on the positive axis (either "D" or "Right" on the keyboard), AND if the facingRight bool is currently false, then set facingRight to True and rotate our character to the exact rotation of 0,0,0.

else if(Input.GetAxis("Horizontal") < 0 && facingRight) : OTHERWISE, if our axis is less then 0 (we're pressing the button negatively), AND facingRight is currently true, then turn facingRight to false and flip our rotation 180 degrees on the Y value.

We flip on rotation instead of doing -1 on the X scale because flipping on the x scale does weird things with colliders!

Now, the fun part!

Attach [the second script] to your camera.

public Vector2 offset; : Modify this value in the inspector to offset the camera from the player by whatever amount you want. Start with small values.

public Transform myObjectToFollow; : Click+Drag your "player" object onto this variable in the inspector in order to grab the position information from it.

public DetectCharacterFlip2DScript characterFlipDetector; : Click+Drag your "player" object onto this variable in the inspector in order to grab the character-flip detector component information (we need the bool from it in real time).

public bool lerpCameraMovementOnX; : Use this to toggle the lerp movement on/off.

public float lerpSpeed; : Use this to set the speed of how fast the camera falls-behind/catches-up.

if (characterFlipDetector.facingRight) : We grab the current value of the "facingRight" boolean from our character flip detector script on our player. If it returns true, then...

tempFollowedObjectWithOffset = new Vector3(myObjectToFollow.position.x + offset.x, myObjectToFollow.position.y + offset.y , transform.position.z); : Phew! This is a long one, eh? What we are doing is grabbing the X position of our player and then ADDING our X offset value to it. We then do the same for the Y value, adding our offset to the current players Y value, leave the Z value alone, and store this all in the "tempFollowedObjectWithOffset" variable to be used later.

else : If we are actually facing left (facingRight returned "false"), then we actually SUBTRACT the players X position from the X offset (ie; we FLIPPED the behavior). We don't do anything different with the Y and Z portions.

if(lerpCameraMovementOnX) : If we have our lerp movement turned on, then we...

*transform.position = Vector3.Lerp(transform.position, tempFollowedObjectWithOffset, lerpSpeed * Time.deltaTime); : Adjust the position of our camera. Using the method "Vector3.Lerp", we take our current position and move towards our previously acquired "tempFollowedObjectWithOffset" position. The speed at which we do this is determined by our lerpSpeed variable multiplied my Time.deltaTime.

else : Otherwise, if we don't want to lerp the movement, we just set the camera position to be exactly the same as the previously acquired "tempFollowedObjectWithOffset" position.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment