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 jast26 · Apr 17, 2019 at 12:58 PM · coroutinelerp

restrict Lerp to only one axis?

good morning; when i try to compile this in VS:

 transform.localScale.x = Vector3.Lerp(posA, inputPosB, fracJourney);

it gives me error cs1612, basically what i need to do is scaling an object along an axis non istantly. I also need to translate it to make it look still (bc there is no shear in unity). this is the rest of the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Lerper : MonoBehaviour
 {
 
     [SerializeField] Vector3 inputPosB = Vector3.zero;
     [SerializeField] Vector3 result;
     [SerializeField] Vector3 avoidBehindCamera;
     private GameObject thisCube;
     private float lengthC;
     [SerializeField] float growFactor = 10f;
     bool isCoroutineRunning = false;
     bool noPosB = true;
 
 
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         PosB();
         noPosB = false;
     }
     void PosB()
     {
         if (Input.GetMouseButtonUp(0))
             if (isCoroutineRunning == false)
             {
                 inputPosB = Camera.main.ScreenToWorldPoint(Input.mousePosition + avoidBehindCamera);
 
 
 
                 Vector3 posA = this.transform.position;
 
                 Vector3 journey = (inputPosB - posA);
                 float journeyLength = Vector3.Magnitude(journey);
                 float journeyAngle = Vector3.Angle(journey, Vector3.right);
                 if (transform.position.y > inputPosB.y) { journeyAngle = 0 - journeyAngle; }
 
 
                 Debug.Log(" : inputPosB" + inputPosB + " : journeyLength " + journeyLength + " :  journeyAngle " + journeyAngle);
                 float startTime = Time.time;
                     StartCoroutine(Scale(startTime,journeyLength,posA,inputPosB));
 
                          }
                      }
 
                  private IEnumerator Scale(float startTime,float journeyLength,Vector3 posA,Vector3 inputPosB)
                      {        
                                 isCoroutineRunning = true;
                                 Debug.Log(isCoroutineRunning);
                                 Debug.Log(journeyLength);
                                 Debug.Log(transform.localScale.x); 
 
                 while (journeyLength > transform.localScale.x )
                 {
                     float distCovered = (Time.time - startTime) * growFactor;
                   //  Debug.Log(distCovered);
                     float fracJourney = distCovered / journeyLength;
                     transform.localScale.x = Vector3.Lerp(posA, inputPosB, fracJourney);
             Debug.Log(transform.localScale.x);
                    yield return null;
                 }
                     
                     isCoroutineRunning = false;
                     Debug.Log(isCoroutineRunning);
             }
 
     }
 
 
 if restricting lerp is not possible, is there a better way to do  this?
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Apr 17, 2019 at 01:21 PM

Sorry but it's completely unclear what you want to do. You seem you try to use the position value of your object and the position of your mouse click somehow as scale for your object. This doesn't make much sense at all. Scale is a unitless factor which is usually "1" while positions are given in world units.


Apart from that localScale is a property of a value type. In order to change just one component of it you have to do it in 3 steps

  • read the whole vector3 into a local variable

  • change the component(s) you want in the local variable

  • assign the local variable back to the property


Though currently you try to assign a Vector3 (which is what Vector3.Lerp returns) to a float value which of course doesn't work.


If you want to scale an object to fit between two points you need several things setup correctly. First of all the origin of the object should be located at the start position. Next you have to know the size of your object at scale 1. Preferably having an object that has already a size of 1 unit at a scale of 1. I've posted an example over here how to rotate and scale a cube (which has it's origin shifted already) so it fits between two points.


When your scale actually matches world units you can directly use your "distCovered" value as scale for your object. Something like this:

 transform.localScale = new Vector3(distCovered, 1, 1);

There are way too many unknowns in your post to give a clear answer.

Comment
Add comment · Show 1 · 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
avatar image jast26 · Apr 17, 2019 at 07:13 PM 0
Share

actually I rewrote everything by following the example you linked and it worked :D Yes actually it wasn't very clear because the object gets instatiated at a position given by another script. I posted another question about that other script (involvingcolliders and rays, completely theoretical no code involved,) i would be very happy if you could check it :) BTW thanks a lot for the effort in understanding what i wanted and thanks again for the example given :D

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

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

LERP gives final value way too early 2 Answers

If statement not being fullfilled until GameObject inspected in inspector. 0 Answers

Howcome this coroutine loops once? 2 Answers

How to use easing inside coroutine to change a float value? 1 Answer

Lerp coroutine not executing 0 Answers


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