Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Transform.localscale

Discussion in 'Scripting' started by spearbeard, May 6, 2018.

  1. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    How do you us this to re scale the objects x y or z axis on collision. I understand it would be a vector3.
    As i would like to be able to make a cube grow by a certain size when colliding with a game pickup. any help would be useful.
     
    Nigel97 likes this.
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Assign a new Vector3 to the transform.localScale.
    Code (csharp):
    1. Vector3 newScale = transform.localScale;
    2. newScale *= 1.2f;
    3. transform.localScale = newScale;
    That example would multiply the scale by 1.2f. You could simply set the x,y,and z components of 'newScale' to your own values, instead, as well.
     
    Nigel97 likes this.
  3. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    Code (CSharp):
    1. public class PickupCollsion : MonoBehaviour {
    2.  
    3.     public GameObject Player;
    4.     public Rigidbody rb;
    5.  
    6.    void OnTriggerEnter( Collider other)
    7.     {
    8.         if(other.tag == "Player") // if the tag of the gameobject is player the pickup gets removed
    9.         {
    10.          
    11.             Destroy(gameObject);
    12.  
    13.         }
    14.     }
    this is how i destroy the object. With what you posted would it be
    would i call player before the new vector3 then put the local scale trasnfromation
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In your example, I would say after you compare the tag, use the code I wrote above. You'd need to use 'other.transform' instead of 'transform' in my code.
     
  5. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    I have got it working. with what you posted on the newScale *= 1.2f; is that the x axis or would i do it



    newScalex *= 1.2f;
    newScaley *= 0.0f;
    newScalez *= 1.2f;
    or
    newScale *= vector3 (1.2f, 0.0f, 1.2f);
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It would depend on what you want. If your player is scaled at 1,1,1 normally, and you only want 1.2, 1, 1.2, perhaps it would be simpler to simply assign that new value. This way, you won't accidentally keep multiplying the value.
    Code (csharp):
    1. other.transform.localScale = new Vector3(1.2f, 1, 1.2f);
    Something like that (can skip making 'newScale' altogether here*).
     
  7. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    THANK YOU so much I have been trying to understand this since Thursday.
    THANK YOU!!!
     
    b10535048 likes this.
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome. :) Take care.
     
    BQT likes this.
  9. noyogiisMYusername

    noyogiisMYusername

    Joined:
    May 18, 2020
    Posts:
    21
    This is regarding a controllable object not a character.
    This localScale inversion works until I rotate the object, and realize that what I am looking for is transform.worldScale

    Code (CSharp):
    1. Vector3 theScale = rb.transform.localScale;
    2.             theScale.y *= -1;
    3.             m_Rb2D.transform.localScale = theScale;
    I looked but did not find a worldScale I found a lossyScale but it is read only.
    Is there a simple way to flip a sprite across the world y axis?
    Not Inline with OP, but the heading is appropriate.
     
  10. Enamati

    Enamati

    Joined:
    Nov 8, 2020
    Posts:
    1
    Hi there. I'm new to unity, so I'm trying to understand the meaning behind certain assignments and invocations, for example, why did you say:

    transform.localScale = new Scale when you've already said
    Vector3 newScale = transform.localScale.
    Isn't that the same thing

    It's probably a stupid question, but I would really like to know
     
  11. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Enamati

    NewScale gets the value of transform's local scale.

    Then newScale value get scaled by 1.2f.

    Finally newScale is assigned as localScale value of transform.