Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Lerping in one direction

Discussion in 'Scripting' started by The_Adventurer, Sep 11, 2015.

  1. The_Adventurer

    The_Adventurer

    Joined:
    Mar 10, 2015
    Posts:
    12
    So I'm trying to create a door which is simply a 5x6x5 cube that shrinks down to 5x1x5 when the player bumps into it. And I want this shrinking action to happen over delta time.

    Vector3.Lerp looks like the command I want, as it transforms the object scale over time. But its the whole object, not just on the Y axis like I want.

    Currently my code isn't doing the 'bump/open' interaction. I'm only trying to initialize my default open door (5x1x5) as a closed door (5x6x5) on start up.

    My current DoorOpen code looks like...


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorOpen : MonoBehaviour {
    5.  
    6.     public int startSize = 6;
    7.     public int minSize = 1;
    8.     public int maxSize = 6;
    9.  
    10.     public float speed = 2.0f;
    11.  
    12.     private Vector3 targetScale;
    13.     private Vector3 baseScale;
    14.     private int currScale;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         baseScale = transform.localScale;
    19.         transform.localScale = baseScale * startSize;
    20.         currScale = startSize;
    21.         targetScale = baseScale * maxSize;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
    27.  
    28.     }
    29. }
    Writing out transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
    as
    transform.localScale.y = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);

    apparently isn't a thing that can be done.

    Note: To get my cube to scale properly I've drop it into an empty gameobject so it scales from the base rather then the center, so I'm scaling the Door gameobject rather then the cube directly.

    Any suggestions?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Lerp will interpolate between any two Vector3s. You can set the target scale to new Vector3(baseScale.x, baseScale.y * maxSize, baseScale.z) if you want to only change Y.

    But there's a deeper problem here, which is you are abusing Lerp. Using Lerp properly for something like this is more trouble than it's worth. So, quit using it.

    Instead, use Vector3.MoveTowards, or if you want to make life easy and only deal with Y, then Mathf.MoveTowards. It's very easy to use, and (unlike Lerp) reasonably hard to misuse.
     
    Iron-Warrior likes this.
  3. The_Adventurer

    The_Adventurer

    Joined:
    Mar 10, 2015
    Posts:
    12
    Okay, my code looks like this now, and compiles. But nothing is happening when I run it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorOpen : MonoBehaviour {
    5.  
    6.     public int startSize = 6;
    7.     public int minSize = 1;
    8.     public int maxSize = 6;
    9.  
    10.     public float speed = 2.0f;
    11.  
    12.     private Vector3 targetScale;
    13.     private Vector3 baseScale;
    14.     private int currScale;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         baseScale = transform.localScale;
    19.         targetScale = new Vector3 (baseScale.x, baseScale.y * maxSize, baseScale.z);
    20.     }
    21.    
    22.     void Update () {
    23.         transform.position = Vector3.MoveTowards (transform.position, targetScale, speed * Time.deltaTime);
    24.  
    25.     }
    26. }
     
  4. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    You're setting the transform.position in your code (in Update), rather than the localScale.
     
  5. The_Adventurer

    The_Adventurer

    Joined:
    Mar 10, 2015
    Posts:
    12
    So...

    Code (CSharp):
    1. void Update () {
    2.         transform.localScale = Vector3.MoveTowards (transform.position, targetScale, speed * Time.deltaTime);
    3.  
    4.     }
    Still doesn't do anything obvious when run.
     
  6. The_Adventurer

    The_Adventurer

    Joined:
    Mar 10, 2015
    Posts:
    12
    Never mind, I'm an idiot. I had the script disabled when it was being buggy.

    It works. Thank you
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Cool! (Hopefully you're not still moving from your position towards your target scale!)
     
  8. The_Adventurer

    The_Adventurer

    Joined:
    Mar 10, 2015
    Posts:
    12
    Yeah, I actually changed it to transform.localScale before I figured out it wasn't enabled. So I went back to turned it back to transform.position just to see what would have happened. Then watched my door fly off into the space. lol

    Thanks again.