Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Schytheron · Nov 29, 2015 at 09:57 AM · objectscalelerpportal

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);
 
         }
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Runalotski · Nov 29, 2015 at 02:10 PM 0
Share

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.

avatar image Schytheron · Nov 29, 2015 at 02:59 PM 0
Share

@Runalotski Well, problem is that I dont instantiate my portals. I simply just transform their position when I place them. What now?

avatar image Runalotski Schytheron · Nov 29, 2015 at 10:01 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase/Decrease Object Scale 1 Answer

Why do some objects have a different scale? 1 Answer

Scale with coroutine and lerp 1 Answer

How to scale an object in editor with absolute or percentual values? 2 Answers

Object sizes from the asset store 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges