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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Scale a gameobject independently in all axes

Discussion in 'Scripting' started by oysbru, Oct 19, 2020.

  1. oysbru

    oysbru

    Joined:
    Oct 19, 2020
    Posts:
    4
    Hi,
    I'm new to Unity and trying to figure out how to scale a cube from a script independently in all axes and both directions.

    This is the data I get from external source:
    Xpositive : int
    Xnegative: int
    Ypositive : int
    Ynegative: int
    Zpositive : int
    Znegative: int

    I have tried to Create Empty object, and adding a cube as child, then adding the script to the empty object and move the origin of the empty object(parent) to a side of the cube, and then I can use this:

    Code (CSharp):
    1. transform.localScale = new Vector3(Xpositive, Ypositive , Zpositive );
    But this only work for one axis, in one direction. How can I make it scale in all axes and both directions independently?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Just a conceptual question: but how would you scale something, anything, independantly in the positive and negative x-direction? They are the same value. Like trying to move an object independantly in the positive and negative x position. It can only exist at one position at a time.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    Sounds like what you want to do is to scale it using both ends of the axis, and then change its position to the centerpoint of those?
    e.g.
    Code (csharp):
    1. float xPosition = transform.position.x + (Xpositive - Xnegative);
    2. float xScale = Xpositive + Xnegative;
     
  4. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I'm not at my pc but I will give my best trying to give you a working script.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Exsample : MonoBehavior
    4. {
    5.     public float scaleSpeed = 5f;
    6.  
    7.     void Update()
    8.     {
    9.         transform.localScale = new Vecror3(transform.localScale.x + scaleSpeed * Time.deltaTime , transform.localScale.y + scaleSpeed * Time.deltaTime, transform.localScale.z + scaleSpeed * Time.deltaTime);
    10.     }
    11. }
    As I said I am not on my pc so I was not able to test it but it should work although you might have to change the + to an *
     
  5. oysbru

    oysbru

    Joined:
    Oct 19, 2020
    Posts:
    4
    Thanks for your reply. I wasn't clear enough when first asking, and didn't provide enough information.
    The pos/neg values are relative to the origin which I also receive.

    So in addition to the previously mentioned data, I also get the origin(position) as integers x, y, z. And rotation for the x axis as an integer of angle. (which I also need to figure out) If that makes any sense?


    Thanks! Why didn't I figure that out myself :rolleyes: I'll try it. As mentioned above, i just need to figure how to rotate as well.

    Thanks, I can try it out and see how it behaves :)