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…
using UnityEngine;
using System.Collections;
public class DoorOpen : MonoBehaviour {
public int startSize = 6;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
// Use this for initialization
void Start () {
baseScale = transform.localScale;
transform.localScale = baseScale * startSize;
currScale = startSize;
targetScale = baseScale * maxSize;
}
// Update is called once per frame
void Update () {
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
}
}
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?
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.
1 Like
Okay, my code looks like this now, and compiles. But nothing is happening when I run it.
using UnityEngine;
using System.Collections;
public class DoorOpen : MonoBehaviour {
public int startSize = 6;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
// Use this for initialization
void Start () {
baseScale = transform.localScale;
targetScale = new Vector3 (baseScale.x, baseScale.y * maxSize, baseScale.z);
}
void Update () {
transform.position = Vector3.MoveTowards (transform.position, targetScale, speed * Time.deltaTime);
}
}
You’re setting the transform.position in your code (in Update), rather than the localScale.
So…
void Update () {
transform.localScale = Vector3.MoveTowards (transform.position, targetScale, speed * Time.deltaTime);
}
Still doesn’t do anything obvious when run.
Never mind, I’m an idiot. I had the script disabled when it was being buggy.
It works. Thank you
Cool! (Hopefully you’re not still moving from your position towards your target scale!)
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.