Search Unity

Strech object between two points

Discussion in 'Scripting' started by aop, Feb 14, 2016.

  1. aop

    aop

    Joined:
    Oct 27, 2015
    Posts:
    5
    Hi guys, i had a problem on pre publication time, one think become one of greatest problem i ever meet. I have a game with some mechanism, u can see it on screenshot, the point of a game is stretch rood between two round
    mechanism. but i cant make it well, it always to far or too close. Very need your help. thx.
    I do it that way :

    _ gameobject1 and 2 its two round mechanism.

    Vector3 mousePos = Input.mousePosition;

    Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
    mousePos.x = mousePos.x - objectPos.x;
    mousePos.y = mousePos.y - objectPos.y;

    // rotate to right direction
    float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

    // stretch stuff
    var distance = (_gameobjec1.transform.position - _gameobjec2.transform.position).magnitude;
    GetComponent<RectTransform>().localScale = new Vector2(distance/3f, 1);

    P.s sorry for my bad english..
     

    Attached Files:

  2. AndreiKubyshkin

    AndreiKubyshkin

    Joined:
    Nov 14, 2013
    Posts:
    213
    Hey!

    are those values in screen space?

    if not, you should probably get on screen positions of this objects
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    in code:

    create a cube that is at the start.

    make the cube look at the end

    Parent the object to that cube.

    use the Z axis on scale on the cube to stretch your object