Search Unity

Discussion Looking for advice/ideas to improve my 2D camera "look ahead" scripting

Discussion in 'Scripting' started by GonzoBonDonzo, Oct 20, 2022.

  1. GonzoBonDonzo

    GonzoBonDonzo

    Joined:
    Jul 29, 2022
    Posts:
    63
    Hi everyone,

    I have another really "dumb" script I wrote for a 2D platformer that mostly does what I want it to do. I say dumb, because it's... not elegant at all.

    It's a camera follow script of the player sprite, and I added code for it to move the camera slightly ahead of the player when they are moving to allow seeing further ahead for potential dangers. The part I need advice on, is the look ahead portion.

    NOW, I know there is a third party solution that probably does exactly what I want (CineMachine), but I would rather just have my own code if possible.

    Here's the code...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollow : MonoBehaviour
    5. {
    6.  
    7.     Transform player; // Declare a Transform component, calling it player
    8.  
    9.     public Vector3 offset; // Declare a Vector3 component, called offset
    10.  
    11.     private Vector3 velocity = Vector3.zero; // define another Vector3, with the function zero (easy way to say Vector3 (0,0,0) )
    12.  
    13.     public float smoothSpeed = .15f; // create a public float smoothSpeed and set it
    14.     public float horizontalValue; // create a public float for a horizontal movement amount
    15.  
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         player = GameObject.FindWithTag("Player").GetComponent<Transform>(); // grab the player's transform values
    21.  
    22.         offset = new Vector3(0f, 0f, -1f); // set a base offset to be used by the look ahead function
    23.     }
    24.  
    25.  
    26.  
    27.     void Update()
    28.     {
    29.  
    30.         // stuff below is for the camera "look ahead"
    31.  
    32.         horizontalValue = Input.GetAxis("Horizontal"); // first, define what the horizontalValue float is equal to. InputAxis can be a positive or negative float number
    33.  
    34.         if (horizontalValue >= 0 && horizontalValue >= 0.1f) // if the horizontalValue is between positive 0 and 0.1 (meaning, moving to the right)...
    35.         {
    36.  
    37.             offset = new Vector3(0.1f, 0f, -1f); // move the offset forward a little
    38.         }
    39.  
    40.         if (horizontalValue >= 0.11f && horizontalValue >= 0.2f) // if the horizontalValue is...
    41.         {
    42.  
    43.             offset = new Vector3(0.2f, 0f, -1f); // move the offset a little more
    44.         }
    45.  
    46.         if (horizontalValue >= 0.21f && horizontalValue >= 0.3f) // repeat
    47.         {
    48.  
    49.             offset = new Vector3(0.3f, 0f, -1f); // repeat
    50.         }
    51.  
    52.         if (horizontalValue >= 0.31f && horizontalValue >= 0.4f)
    53.         {
    54.  
    55.             offset = new Vector3(0.4f, 0f, -1f);
    56.         }
    57.  
    58.         if (horizontalValue >= 0.41f && horizontalValue >= 0.5f)
    59.         {
    60.  
    61.             offset = new Vector3(0.5f, 0f, -1f);
    62.         }
    63.  
    64.         if (horizontalValue >= 0.51f && horizontalValue >= 0.6f)
    65.         {
    66.  
    67.             offset = new Vector3(0.6f, 0f, -1f);
    68.         }
    69.  
    70.         if (horizontalValue >= 0.61f && horizontalValue >= 0.7f)
    71.         {
    72.  
    73.             offset = new Vector3(0.7f, 0f, -1f);
    74.         }
    75.  
    76.         if (horizontalValue >= 0.71f && horizontalValue >= 0.8f)
    77.         {
    78.  
    79.             offset = new Vector3(0.8f, 0f, -1f);
    80.         }
    81.  
    82.         if (horizontalValue >= 0.81f && horizontalValue >= 0.9f)
    83.         {
    84.  
    85.             offset = new Vector3(0.9f, 0f, -1f);
    86.         }
    87.  
    88.         if (horizontalValue >= 0.91f && horizontalValue >= 1f)
    89.         {
    90.  
    91.             offset = new Vector3(1.2f, 0f, -1f);
    92.         }
    93.  
    94.  
    95.         if (horizontalValue <= 0 && horizontalValue <= -0.1f) // if the horizontalValue is between 0 and negative 0.1 (moving to the left)...
    96.         {
    97.  
    98.             offset = new Vector3(-0.1f, 0f, -1f); // move the offset forward a little
    99.         }
    100.  
    101.         if (horizontalValue <= 0.11f && horizontalValue <= -0.2f) // if the horizontalValue is...
    102.         {
    103.  
    104.             offset = new Vector3(-0.2f, 0f, -1f); // move the offset a little more
    105.         }
    106.  
    107.         if (horizontalValue <= 0.21f && horizontalValue <= -0.3f) // repeat
    108.         {
    109.  
    110.             offset = new Vector3(-0.3f, 0f, -1f); // repeat
    111.         }
    112.  
    113.         if (horizontalValue <= 0.31f && horizontalValue <= -0.4f)
    114.         {
    115.  
    116.             offset = new Vector3(-0.4f, 0f, -1f);
    117.         }
    118.  
    119.         if (horizontalValue <= 0.41f && horizontalValue <= -0.5f)
    120.         {
    121.  
    122.             offset = new Vector3(-0.5f, 0f, -1f);
    123.         }
    124.  
    125.         if (horizontalValue <= 0.51f && horizontalValue <= -0.6f)
    126.         {
    127.  
    128.             offset = new Vector3(-0.6f, 0f, -1f);
    129.         }
    130.  
    131.         if (horizontalValue <= 0.61f && horizontalValue <= -0.7f)
    132.         {
    133.  
    134.             offset = new Vector3(-0.7f, 0f, -1f);
    135.         }
    136.  
    137.         if (horizontalValue <= 0.71f && horizontalValue <= -0.8f)
    138.         {
    139.  
    140.             offset = new Vector3(-0.8f, 0f, -1f);
    141.         }
    142.  
    143.         if (horizontalValue <= 0.81f && horizontalValue <= -0.9f)
    144.         {
    145.  
    146.             offset = new Vector3(-0.9f, 0f, -1f);
    147.         }
    148.  
    149.         if (horizontalValue <= 0.91f && horizontalValue <= -1f)
    150.         {
    151.  
    152.             offset = new Vector3(-1.2f, 0f, -1f);
    153.         }
    154.     }
    155.  
    156.  
    157.     void FixedUpdate()
    158.     {
    159.         Vector3 camPosition = player.position + offset; // create and set a Vector 3 value called camPosition. camPosition is a combination of the player's transform position and the offset position
    160.            
    161.         transform.position = Vector3.SmoothDamp(transform.position, camPosition, ref velocity, smoothSpeed); // position of the transform of the camera will equal a Vector3 with SmoothDamp function (and we need 4 values for SmoothDamp to calculate)
    162.  
    163.     }
    164.  
    165.  
    166.  
    167.  
    168. }
     
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Have you got a video of what it currently does?

    by elegant could you not make all these guys else if? Or do they all need to be checked and adjusted on the same frame?
     
  3. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
  4. GonzoBonDonzo

    GonzoBonDonzo

    Joined:
    Jul 29, 2022
    Posts:
    63
    I can make a video of it, and yes, I am hoping to get an idea on how I can, instead of all the ifs that check the horizontalValue (and moving the offset depending on the horizontal range), have a calculation or a smooth ramp up to get the offset to be ahead of the player.

    I'll post the video in just a sec
     
  5. GonzoBonDonzo

    GonzoBonDonzo

    Joined:
    Jul 29, 2022
    Posts:
    63
    Here's a video of how it is working right now. I'm not 100% happy (I want to try and eliminate the jerkiness, make the offset a little bigger at max movement, and try and add a movement delay, is in, the camera will only move ahead after some a small delay), but it's mostly doing what I am trying to do
     
    Last edited: Oct 22, 2022
  6. GonzoBonDonzo

    GonzoBonDonzo

    Joined:
    Jul 29, 2022
    Posts:
    63
    In case anyone is interested, I was able to finally create a very smooth, clean 2D follow + look ahead camera system. I tried a bunch of different examples for smoothly changing a float and cobbled together this. I think I'm 95% happy with it (though, maybe I can get the "return" motion to be a bit faster)




    here's the code if anyone wants it

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class newCameraFollow : MonoBehaviour
    6. {
    7.     Transform player; // declare a transform component (to be that of the player's character)
    8.     PlayerMovement pm; // declare the PlayerMovement script as pm
    9.  
    10.     public float xOffset; // declare a float to be used as the x axis offset
    11.     public float yOffset; // declare a float to be used as the y axis offset
    12.  
    13.     public float followSpeed; // declare a float called Follow Speed
    14.  
    15.     void Start()
    16.     {
    17.         player = GameObject.FindWithTag("Player").GetComponent<Transform>(); // define what the declared transform component (of the player) equals
    18.         pm = GameObject.FindWithTag("Player").GetComponent<PlayerMovement>(); // define what the declared PlayerMovement component equals
    19.         xOffset = 0f; // set what the default xOffset is
    20.         yOffset = .7f; // set what the default yOffset is
    21.         followSpeed = 12f; // set what the Follow Speed is
    22.     }
    23.  
    24.  
    25.     void Update()
    26.  
    27.         // below setup is for the "look up" feature. This game uses it's own key for jumping, and the up key is free for other things, so it looks up
    28.     {
    29.         if (Input.GetKeyDown("up")) // if the player presses the "up arrow" key
    30.         {
    31.             yOffset = 2f; // the y axis offset (vertical axis) is moved up, allowing the player to see further above
    32.  
    33.         }
    34.  
    35.         else if (Input.GetKeyUp("up")) // when the "up arrow" key is released...
    36.         {
    37.             yOffset = .7f; // return to default
    38.  
    39.         }
    40.  
    41.         // below setup for the "look ahead" camera offset
    42.         // first, we have a public horizontal velocity float in our movement script (horizontalVel) which in that script is in Update as horizontalVel = Input.GetAxis("Horizontal");
    43.  
    44.  
    45.         if (pm.horizontalVel >= 0.11f) // if horizontalVel is greater than .11f (meaning, moving to right)
    46.         {
    47.             xOffset = Mathf.Lerp(xOffset, 2.55f, 1 * Time.deltaTime); // the xOffset will equal a Mathf.Lerp value (current value, target value, and time factor). The target value is 2.55
    48.  
    49.         }
    50.  
    51.         else if (pm.horizontalVel == 0) //if horizontalVel is 0f
    52.         {
    53.             xOffset = Mathf.Lerp(xOffset, 0f, 1 * Time.deltaTime); //do a new mathf.lerp, with the target value 0f
    54.         }
    55.  
    56.         if (pm.horizontalVel <= -0.11f) // now, if the player is moving to the left (negative horizontalVel)
    57.         {
    58.             xOffset = Mathf.Lerp(xOffset, -2.55f, 1 * Time.deltaTime); // the target offset is a negative value
    59.  
    60.         }
    61.  
    62.     }
    63.  
    64.     void FixedUpdate()
    65.     {
    66.         float xTarget = player.position.x + xOffset; // create a float called xTarget and have it equal to the player x position + xoffset
    67.         float yTarget = player.position.y + yOffset; // create a float called yTarget and have it equal to the player y position + yoffset
    68.  
    69.         float xNew = Mathf.Lerp(transform.position.x, xTarget, Time.deltaTime * followSpeed); //a new x position using a linear calculation
    70.         float yNew = Mathf.Lerp(transform.position.y, yTarget, Time.deltaTime * followSpeed); //same but for y
    71.  
    72.         transform.position = new Vector3(xNew, yNew, transform.position.z); // camera transform position will equal a new Vector3 set(x value, y value, and z value)
    73.     }
    74.  
    75.  
    76.  
    77. }