Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Setting local scale

Discussion in 'Scripting' started by Corva-Nocta, May 20, 2019.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    461
    I am spawning weapons for my enemies but I need them to scale with the enemy. I was previously just copying the scale of the enemy, but this causes major issues at higher sizes. So now I'm trying to cap thebscale but having issuss.

    Code (csharp):
    1.  
    2. if (weapon.transform localScale > 1.5)
    3. {
    4.    weapon.transform.localScale = 1.5f;
    5. }
    6. else
    7. {
    8.    weapon.localScale = transform.localScale
    9. }
    now I know I need to switch the 1.5f to a vector3, but I'm not sure what that would look like. I'm also not sure if my if statement is even correcr.

    Can anyone point me in the right direction?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,714
    Whether it's the right approach or not really depends on the specifics of your game. You might need to add a component to every object that can hold weapons, where the component only provides a scale value you want to use for that particular object.

    As for the code, you could test the magnitude of the localScale to see if it's larger than 1.5. And for setting the localScale, you could set it to `Vector3.one * 1.5f'.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    831
    If you're sure that the X, Y, and Z components of the scale are always going to be equal, then you can just pick one of them to test if it's greater than 1.5. (As dgoyette says, you can set the scale to Vector3.one * 1.5f to make all components 1.5.)

    If there's a possibility that they might not be equal, then you're going to need to further define what you want. Do you want to cap each component separately? Do you want to shrink the entire vector proportionately until the magnitude is within some threshold? Something else?

    (You can test the vector's overall length using the "magnitude" property, although when practical you should use sqrMagnitude instead because it's a little more efficient.)
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    461
    Both sound like great pieces of advice.

    I should have probably put more info on the code (sadly I'll be away from my project for a few hours, but I figure more info couldn't help) currently the weapon is being spawned
    Code (csharp):
    1. GameObject weapon = Instantiate(refence to weapon model);
    Then immediately I do some adjusting with rotation and other code stuff not really relevant to this post. Then I want to adjust the scale.

    So this code
    Code (csharp):
    1. weapon.localScale = transform.localScale;
    works without fail. The spawned weapon will scale to the same scale as the enemy that it spawned onto. Also there will only ever be one enemy in the game at a time so I'm not worried about multiple codes. The problem comes when I start having the scale bigger than about 1.5, its not that it gets buggy its just that the weapons get scaled to a hilarious size. Hence the idea of the scale cap.

    So the biggest problem is first I need to be able to check if the scale is bigger than 1.5. x, y, and z should all be the exact same scale, so really I could just check the scale of the x and be fine. Can I just use transform.localScale.x?

    Of yes, do I need to set the tramsform.localscale of x, y, and z all separately? Or can I do it in one line of code?
     
    Last edited: May 21, 2019
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    461
    So I tried doing:
    Code (csharp):
    1. weapon.transform.localScale.x = 1.5f;
    (And again for y and z) but I get an odd error.

    "Cannot modify the return value 'Transform.localScale' because it is not a variable
     
  6. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    461
    I'm getting closer, I think.

    Code (csharp):
    1. if (weapon.transform.localScale.x > 1.5f)
    2. {
    3.    weapon.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
    4. }
    5. else
    6. {
    7.    weapon.transform.localScale = transform.localScale;
    8. }
    I'm not getting any error, but, the scaling will still go above 1.5 for enemies that are scaled larger than 1.5.

    Any thoughts?
     
  7. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    461
    I finally figured it out! Turns out the above code is correct, but I was checking the weapon.transform.scale and not the transform.scale. but the rest works!

    Thanks for letting me think out loud to work it out haha