Transform.localscale

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.

1 Like

Assign a new Vector3 to the transform.localScale.

Vector3 newScale = transform.localScale;
newScale *= 1.2f;
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.

1 Like
public class PickupCollsion : MonoBehaviour {

    public GameObject Player;
    public Rigidbody rb;

   void OnTriggerEnter( Collider other)
    {
        if(other.tag == "Player") // if the tag of the gameobject is player the pickup gets removed
        {
         
            Destroy(gameObject);

        }
    }

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

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.

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);

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.

other.transform.localScale = new Vector3(1.2f, 1, 1.2f);

Something like that (can skip making ‘newScale’ altogether here*).

THANK YOU so much I have been trying to understand this since Thursday.
THANK YOU!!!

1 Like

You’re welcome. :slight_smile: Take care.

1 Like

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

Vector3 theScale = rb.transform.localScale;
            theScale.y *= -1;
            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.

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

@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.