Search Unity

Scale 2D sprite moving vertically to "simulate depth"

Discussion in '2D' started by Foewlett, Mar 26, 2020.

  1. Foewlett

    Foewlett

    Joined:
    Mar 21, 2020
    Posts:
    2
    Super-duper new to this whole world but me and my buddy decided to give it a go anyways.
    So, here's our situation:

    We're making a 2D adventure game, along the lines of Monkey Island, Kings Quest, Darkseed etc.
    While we want it to be mainly horizontal movement we want to some degree be able to move vertically and as such we would want the sprite to become "smaller" as it moves "away" or "upwards".
    Since it is ortographic in 2D, I guess (please correct me if I'm wrong), we have to simluate this depth using scaling tied to the keyboard input moving us vertically. I can't find any tips, guides or anything to help me figure this out. Any tips at all from anyone out there?
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    each object has a scale property, you can mess with that to active what you want.

    but dont scale the original object (eg. player), scale the subobject holding the sprite/images. the reason for this being you will want to remember the location your player is at on the scene without it being modified by scale.
     
  3. Foewlett

    Foewlett

    Joined:
    Mar 21, 2020
    Posts:
    2
    Thank you for the reply!
    Do you know if there is a straighr forward way to bind this size to the vertical location or perhaps make the vertical controller (up and down arrow) scale the object? (sorry if these question are stupid, haha)
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    This is something I wrote for a prototype at some point. It's not very fancy but maybe it can work for you.

    It scales an object based on it's location between two world space Y positions, with some gizmos drawn in the scene to set it up.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HorizonEffect : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private float _horizonY = 1;
    7.     [SerializeField]
    8.     private float _originalScaleAtY = -1;
    9.  
    10.     private float   _currentY;
    11.     private float   _previousY;
    12.     private Vector3 _regularScale;
    13.  
    14.     private void Awake()
    15.     {
    16.         _regularScale = transform.localScale;
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         _currentY = transform.position.y;
    22.  
    23.         if (Mathf.Approximately(_currentY, _previousY))
    24.         {
    25.             return;
    26.         }
    27.  
    28.         float normalizedDistance = Mathf.InverseLerp(_horizonY, _originalScaleAtY, _currentY);
    29.         transform.localScale = Vector3.Lerp(Vector3.zero, _regularScale, normalizedDistance);
    30.  
    31.         _previousY = _currentY;
    32.     }
    33.  
    34.     private void OnDrawGizmosSelected()
    35.     {
    36.         if (Camera.main != null)
    37.         {
    38.             Gizmos.color = Color.red;
    39.             float cameraLeft  = Camera.main.ViewportToWorldPoint(Vector3.zero).x;
    40.             float cameraRight = Camera.main.ViewportToWorldPoint(Vector3.one).x;
    41.             Gizmos.DrawLine(new Vector3(cameraLeft, _horizonY, 0), new Vector3(cameraRight, _horizonY, 0));
    42.  
    43.             Gizmos.color = Color.green;
    44.             Gizmos.DrawLine(new Vector3(cameraLeft, _originalScaleAtY, 0), new Vector3(cameraRight, _originalScaleAtY, 0));
    45.         }
    46.     }
    47. }