- Home /
Why won't my object grow?
Hi, Im making a portal clone game and when I shoot my portal gun to place a portal on the wall I want the portal to gradually grow in size and then stop growing when it reaches the desired size. I figured I could do this with a lerp function but the portal does not grow for some reason (just appears small when I place it on the wall). I have no idea what Im doing wrong. Help!
using UnityEngine;
using System.Collections;
public class PortalShoot : MonoBehaviour {
public GameObject leftPortal;
public GameObject rightPortal;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
ShootPortal(leftPortal);
}
if (Input.GetMouseButtonDown (1)) {
ShootPortal(rightPortal);
}
}
void ShootPortal(GameObject portal){
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = Camera.main.ScreenPointToRay (new Vector3 (x, y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit) && hit.collider.tag != "Portal" && hit.collider.tag == "Wall"){
portal.transform.position = hit.point;
portal.transform.rotation = Quaternion.LookRotation(hit.normal);
portal.transform.localScale = Vector3.Lerp(new Vector3(0.1f,0.1f,0.1f), new Vector3(2.5f,3.5f,0.01f), Time.deltaTime * 0.5f);
}
}
}
you would need to carry over the time element as the Time.deltaTime * 0.5f will give the same number each frame.
Add a variable
float lerpTime = 0;
as a global variable then you want to add to change the code inside the raycast to
if(Physics.Raycast(ray, out hit) && hit.collider.tag != "Portal" && hit.collider.tag == "Wall"){
portal.transform.position = hit.point;
portal.transform.rotation = Quaternion.LookRotation(hit.normal);
//added lerpTime here
portal.transform.localScale = Vector3.Lerp(new Vector3(0.1f,0.1f,0.1f), new Vector3(2.5f,3.5f,0.01f), lerpTime);
//now lerpTime will increase over serveral frames
lerpTime += (Time.deltaTime * 0.5f)
}
also the portal will only grow when "ShootPortal" runs and the raycast returns true which means you will need to keep clicking the mouse to make it grow.
try moving the growPortal part of the script to the portal it self. if you instantiate the portal add ad the grow portion of the code to a new script in the update section so it will grow it self when created.
@Runalotski Well, problem is that I dont instantiate my portals. I simply just transform their position when I place them. What now?
You would add part of the script to reset the lerpTime back to zero when you move the portal, this will then allow the portal to grow again.
Answer by maccabbe · Nov 29, 2015 at 03:14 PM
In general Lerp(from, to, t) = from*(1-t)+to*t
So for floats
Lerp(1, 2, 0) = 1*1+2*0 = 1+0=1, or from
Lerp(1, 2, 0.5) = 1*0.5+2*0.5 = 0.5+1=1.5 or halfway between from and to
Lerp(1, 2, 1.0) = 1*0+2*1 = 0+2=2, or to
In your example you are using
Vector3.Lerp(new Vector3(0.1f,0.1f,0.1f), new Vector3(2.5f,3.5f,0.01f), Time.deltaTime * 0.5f);
Let's say your frames per second fluctuates around 25 to 100. Then Time.deltaTime will flucuate from 0.01 to 0.04. So the return of lerp will fluctuate between from*0.99+to*0.01 and from*0.96+to*0.04. Since your from and to are constants this means you portal will always remain close to the from sizes. In addition you only lerp once per click, when the portal is shot so the portal never resizes.
Two common ways to use Lerp are to have a changing t (third variable) or a changing from (first variable). For instance
float time;
void Update()
{
if (Input.GetMouseButtonDown (0)) {
time=0;
}
time+=Time.deltaTime;
Debug.Log(Mathf.Lerp(1, 2, time));
}
and
float from;
void Update()
{
if (Input.GetMouseButtonDown (0)) {
from=1;
}
from=Mathf.Lerp(from, 2, Time.deltaTime);
Debug.Log(from);
}
Personally I stick to changing t (third variable) as, to me, it feels like if you're changing from (first variable) then you shouldn't be using lerp in the first place but rather another function like a polynomial, exponential, or sin.