Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shrinking and growing

Discussion in 'Scripting' started by DariusdXm, Nov 19, 2021.

  1. DariusdXm

    DariusdXm

    Joined:
    Apr 22, 2021
    Posts:
    4
    Hey guys! I’m trying to create a script where if you interract with an object you can grow or shrink the player, and you get the text “Press [E] to grow/shrink”. Hope you guys can help me.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    Well, what do you have so far?
     
  3. DariusdXm

    DariusdXm

    Joined:
    Apr 22, 2021
    Posts:
    4
    I got this code, but I can't find the right parameters on the player ShrinkOrGrow code.



    Instead of shrinking, the player grows and the camera goes to its feet.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ShrinkOrGrow : MonoBehaviour {
    5.     //Public variables
    6.     //When triggering shrink() GameObject will scale down to [shrunkScale] over [time]
    7.     public Vector3 shrunkScale;
    8.     //When triggering grow() GameObject will scale up to [grownScale] over [time]
    9.     public Vector3 grownScale;
    10.     //Growth/shriuking time (MILLISECONDS)
    11.     public float time;
    12.     //Private variables
    13.     private bool active = false;
    14.     private Vector3 originalScale;
    15.     //private bool shrinking;
    16.     private float timePassed = 0.0f;
    17.     private float progress;
    18.     private enum State { SHRUNKEN, ORIGINAL_SIZE, GROWN }
    19.     private State state = State.ORIGINAL_SIZE;
    20.     private enum Actions { SHRINKING, NORMALIZING, GROWING }
    21.     private Actions action = Actions.NORMALIZING;
    22.     // Use this for initialization
    23.     void Start ()
    24.     {
    25.         originalScale = transform.localScale;
    26.     }
    27.     private void FixedUpdate()
    28.     {
    29.         if (active)
    30.         {
    31.             timePassed += Time.deltaTime * 1000.0f;
    32.             //0 - 1 with time
    33.             progress = (timePassed / time);
    34.         }
    35.         switch (action) {
    36.             case Actions.NORMALIZING:
    37.                 switch (state) {
    38.                     case State.SHRUNKEN:
    39.                         transform.localScale = new Vector3(
    40.                             (1 - progress) * shrunkScale.x + progress * originalScale.x,
    41.                             (1 - progress) * shrunkScale.y + progress * originalScale.y,
    42.                             (1 - progress) * shrunkScale.z + progress * originalScale.z
    43.                         );
    44.                         break;
    45.                     case State.GROWN:
    46.                         transform.localScale = new Vector3(
    47.                             (1 - progress) * grownScale.x + progress * originalScale.x,
    48.                             (1 - progress) * grownScale.y + progress * originalScale.y,
    49.                             (1 - progress) * grownScale.z + progress * originalScale.z
    50.                         );
    51.                         break;
    52.                     default:
    53.                         break;
    54.                 }
    55.                 break;
    56.             case Actions.GROWING:
    57.                 switch (state)
    58.                 {
    59.                     case State.SHRUNKEN:
    60.                         transform.localScale = new Vector3(
    61.                             (1 - progress) * shrunkScale.x + progress * grownScale.x,
    62.                             (1 - progress) * shrunkScale.y + progress * grownScale.y,
    63.                             (1 - progress) * shrunkScale.z + progress * grownScale.z
    64.                         );
    65.                         break;
    66.                     case State.ORIGINAL_SIZE:
    67.                         transform.localScale = new Vector3(
    68.                             (1 - progress) * originalScale.x + progress * grownScale.x,
    69.                             (1 - progress) * originalScale.y + progress * grownScale.y,
    70.                             (1 - progress) * originalScale.z + progress * grownScale.z
    71.                         );
    72.                         break;
    73.                     default:
    74.                         break;
    75.                 }
    76.                 break;
    77.             case Actions.SHRINKING:
    78.                 switch (state)
    79.                 {
    80.                     case State.ORIGINAL_SIZE:
    81.                         transform.localScale = new Vector3(
    82.                             (1 - progress) * originalScale.x + progress * shrunkScale.x,
    83.                             (1 - progress) * originalScale.y + progress * shrunkScale.y,
    84.                             (1 - progress) * originalScale.z + progress * shrunkScale.z
    85.                         );
    86.                         break;
    87.                     case State.GROWN:
    88.                         transform.localScale = new Vector3(
    89.                             (1 - progress) * grownScale.x + progress * shrunkScale.x,
    90.                             (1 - progress) * grownScale.y + progress * shrunkScale.y,
    91.                             (1 - progress) * grownScale.z + progress * shrunkScale.z
    92.                         );
    93.                         break;
    94.                     default:
    95.                         break;
    96.                 }
    97.                 break;
    98.             default:
    99.                 break;
    100.         }
    101.         if (progress >= 1)
    102.         {
    103.             active = false;
    104.             switch (action) {
    105.                 case Actions.GROWING:
    106.                     state = State.GROWN;
    107.                     break;
    108.                 case Actions.NORMALIZING:
    109.                     state = State.ORIGINAL_SIZE;
    110.                     break;
    111.                 case Actions.SHRINKING:
    112.                     state = State.SHRUNKEN;
    113.                     break;
    114.             }
    115.         }
    116.     }
    117.     public void grow()
    118.     {
    119.         active = true;
    120.         action = Actions.GROWING;
    121.         timePassed = 0.0f;
    122.     }
    123.     public void shrink()
    124.     {
    125.         active = true;
    126.         action = Actions.SHRINKING;
    127.         timePassed = 0.0f;
    128.     }
    129.     public void originalSize()
    130.     {
    131.         active = true;
    132.         action = Actions.NORMALIZING;
    133.         timePassed = 0.0f;
    134.     }
    135. }
    136.  
     
  4. DariusdXm

    DariusdXm

    Joined:
    Apr 22, 2021
    Posts:
    4
    Here are some photos of the scene
     

    Attached Files:

  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    Your progress check should be before your switch statement, not after. In the example you provided, the time is too short for the correct calculations, as it's expecting time in ms, and you only gave it 2. That means your progress starts off greater than 1, and from your first image, you can clearly see that it set the scale to negative values from that.
     
  6. DariusdXm

    DariusdXm

    Joined:
    Apr 22, 2021
    Posts:
    4
    I increased the time and it seems to be working better but not quite perfect. I haven't understood everything you said. Could you try and explain it easier for a begineer :)). Also, how could I make so I have to press a button on the cube to perform the action. Sorry for the stupid begineer questions, but thanks for your help so far!