Search Unity

How to stretch/scale simply object and attach to another ?

Discussion in 'Getting Started' started by Avenged90x, Oct 11, 2017.

  1. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Hi guys!

    I'm learning C#, but I'm very, very beginner at it..
    I learned from unity tutorials, books about programming c#, but i decides to make something of my own.

    So i have a bit struggle with my problem. I want to make a mechanic, where I can stretch or scale a 'cube' and than land it on another platform by dragging / swipe gesture/movement of mouse. I made this code:

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3.  
    4.     private Vector3 swipeStart;
    5.     private Vector3 swipeEnd;
    6.     private float ratio = -0.002f;
    7.     private bool tap;
    8.     Vector3 scale;
    9.     int min_X = 1;
    10.  
    11.     void Update()
    12.     {
    13.         if(Input.GetMouseButtonDown(0))
    14.         {
    15.             tap = true;
    16.             swipeStart = Input.mousePosition;
    17.             Debug.Log(Input.mousePosition);
    18.         }
    19.         else if(Input.GetMouseButtonUp(0))
    20.         {
    21.             tap = false;
    22.             swipeEnd = Input.mousePosition;
    23.             Debug.Log(Input.mousePosition);
    24.             Reset();
    25.         }
    26.         if(tap)
    27.          
    28.         {
    29.             Vector3 newScale = transform.localScale * ((swipeEnd - swipeStart)* ratio).magnitude;
    30.             transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, newScale.z);
    31.         }
    32.         //if(Input.GetAxis("Mouse X") > 0)
    33.         //{
    34.         //    tap = true;
    35.         //}
    36.        
    37.     }
    38.  
    39.     private void Reset()
    40.     {
    41.         swipeStart = swipeEnd = Vector3.zero;
    42.     }
    43.  
    44.     //public Vector3 SwipeDelta { get { return swipeDelta; } }
    45.  
    46.     void Start ()
    47.     {
    48.        /* if (scale.x <= 1)
    49.         {
    50.             transform.localScale = new Vector3(1.0f, transform.localScale.y, transform.localScale.z);
    51.         }
    52.        
    53.  
    54.         transform.localScale = new Vector3(1,1,1);
    55.         */
    56.     }
    And it works already with stretching the object in Z axis. I made also an empty gameobject, which has that script, and as a child i attached the 'Cube' which is the visual game Object, and we can see how it stretch. So the pivot point of the cube is the empty gameObject as a parent - and it allows to stretch/scale the cube to the 1 direction (not in both directions as a standard scaling tool).

    But now i want to attach that gameobject with cube to another platform which you can see on the picture below. How to do that?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It depends on what you mean by "attach". That doesn't really need to mean anything except "my code keeps these objects together and makes sure to always move them the same way."

    But if you want Unity to help out with that, then you can just change the transform parent (via Transform.SetParent), so that it is located within the other object in the scene hierarchy. Then ensure you move only the parent, and the children objects will move with it.
     
  3. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Ok, maybe I didnt explain that precisely :D.

    I want to make something like on the pictures:
    At the beginning, the orange cube is on the left grey platform. When I make a swipe gestuer / slide / drag move of the mouse - the orange cube should stretch/extrude in the direction of swipe move. So it will be 6 directions at all for this cube movement. And when the orange box will collide with 'collider box' of another platform - the box should be automatically attatched to this next platform as with previous - than it'll back to the original scale,sizes - the shape of the cube.

    And i have the second question - how to make the scaling the cube by swipe / dragging mouse proportional to length of swipe? When i start making swipe - cube starts scaling in x axis for example.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Wow, that sounds like a rather tricky program!

    The basic answer is the same: to attach to something else, you would reparent it to a new empty gameObject at the new attachment point (while keeping its world position the same), and then scale that until your stretchy thing is a cube again.

    But the details... particularly in determining which way they're swiping to decide which axis to scale, etc... not trivial.
     
  5. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    You're right.. it's difficult stuff, what i've invented :D..
    Its need to make functions, conditions, to chech the direction and length of vector of swipe move.. Checking many angles of line/swipe between two points - in X,Y axis (because we can make only 2D moves on the screen by finger or mouse) and converting them to movement of the cube.. hard stuff for me :D I made only 3 (2,5) tutorials for beginners in unity and c#, and 2 books about c# and programming, not too much..

    So maybe now, I'll try to make much easier version of prototype - just add arrow buttons, and when you click on the arrow button (in additive of X axis direction) the 'cube'/player will extrude in that direction to move to another platform. So, thats the way i think I can figure out how to make :D..

    Than, when the idea of the concept for this game will be good, and playable, i'll think about converting that into the swipe controlling :).

    I made also the collision and changing position :).
    Thanks for replies :D
     
    JoeStrout likes this.