Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Scale + Translate transform at same time

Discussion in 'Scripting' started by adentutton, May 27, 2016.

  1. adentutton

    adentutton

    Joined:
    Mar 24, 2013
    Posts:
    34
    Hi guys

    I am looking for a solution to create a ball movement which transforms by scale at the same time as movement (simulating a 2d ball flight). At the moment this is an example of my code which only contains the translate movement in a coroutine.

    Code (CSharp):
    1. IEnumerator ServeA () {
    2.  
    3.         while (Vector2.Distance(ball.transform.position, serve_left_position[4]) > 0.01f) {
    4.             ball.transform.position = Vector2.MoveTowards(ball.transform.position, serve_left_position [4], transitionspeed * speedmodifier * Time.deltaTime);
    5.             yield return null;
    6.         }
    Is there a way to combine it easily into this script or will I have rework the code? Maybe even recommend an asset which will make my job easier?

    Thanks in advance!

    Aden
     
  2. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    Perhaps something like this...

    Code (csharp):
    1.  
    2. float t = 0.0f;
    3.  
    4. while( t < 1.0f )
    5. {
    6.       ball.transform.position = Vector2.Lerp( originalPostion, newPosition, t );
    7.       ball.transform.scale = Vector2.Lerp( originalScale, newScale, t );
    8.       t += Time.deltaTIme;
    9.       yield return null;
    10. }
    11.  
     
  3. vikankraft

    vikankraft

    Joined:
    Feb 25, 2016
    Posts:
    88
    Scaling and transform are done with 2 separate matrices but you can bake them together without a problem.
    Rotating and translating can not be done at the same time since depending on what you do first you will get different results.
     
  4. lexws33

    lexws33

    Joined:
    Apr 4, 2019
    Posts:
    1
    Technically, transform and scale can be done in the same matrix, where the last column is the translation vector and the diagonal is the scalling vector.

    NOTE: The dimension of the matrix should be one more dimension of the vectors you are working with.
     
unityunity