Advertisement
gameDevTeacher

Squeeze & Stretch Unity

Oct 18th, 2020 (edited)
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. /*
  2.     THIS CODE REQUIRES A CHARACTER CONTROLLER FROM WHICH YOU GET THE OBJECTS VELOCITY AND CHECK WETHER THE OBJECT IS GROUNDED.
  3. */
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7.  
  8. public class SqueezeStretch : MonoBehaviour
  9. {
  10.     public Transform target;
  11.     private CharacterController2D _Controller;
  12.  
  13.     public bool canStretch = true;
  14.  
  15.     [Space(5)]
  16.     [Header("Lerp & Stretch")]
  17.     [SerializeField] private float _lerpTime = .05f;
  18.     [SerializeField] private Vector2 _xStretch = new Vector2(0.5f, 1.5f);
  19.     [SerializeField] private Vector2 _yStretch = new Vector2(1.5f, 0.5f);
  20.  
  21.     private void Start()
  22.     {
  23.         _Controller = GetComponent<CharacterController2D>();
  24.     }
  25.  
  26.  
  27.     // Update is called once per frame
  28.     void Update()
  29.     {
  30.         if (target.localScale.x != 1|| target.localScale.y != 1)
  31.         {
  32.             target.localScale = new Vector2(Mathf.Lerp(target.localScale.x, 1, _lerpTime), Mathf.Lerp(target.localScale.y, 1, _lerpTime));
  33.         }
  34.        
  35.         if (Input.GetButtonDown("Jump") && !canStretch)
  36.         {
  37.             target.localScale = new Vector2(_xStretch.x, _xStretch.y);
  38.             canStretch = true;
  39.         }
  40.         else if (_Controller.grounded && canStretch)
  41.         {
  42.             if (_Controller.velocity.y < -8f)
  43.             {
  44.                 target.localScale = new Vector2(_yStretch.x ,_yStretch.y);
  45.             }
  46.             canStretch = false;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement