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. Dismiss Notice

LookAt and scale

Discussion in 'Scripting' started by Dytdyt, Dec 4, 2014.

  1. Dytdyt

    Dytdyt

    Joined:
    Nov 9, 2014
    Posts:
    7
    Hi guys

    My code is this:
    Code (CSharp):
    1.  
    2. [INDENT]usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassSuspensionModelling : MonoBehaviour {
    6.  
    7. public float dist;
    8.  
    9. //Usethisforinitialization
    10. void Start () {
    11.  
    12. }
    13.  
    14. //Updateiscalledonceperframe
    15. voidUpdate () {
    16. dist = Vector3.Distance (GameObject.Find ("Wheel1").transform.position, GameObject.Find ("BackSusTop").transform.position);
    17. gameObject.transform.position = (((GameObject.Find ("Wheel1").transform.position)+(GameObject.Find ("BackSusTop").transform.position))/2);
    18. gameObject.transform.LookAt (GameObject.Find ("BackSusTop").transform.position); // TRANSFORM
    19. gameObject.transform.localScale = newVector3(dist, 0.1f,1f); // SCALE
    20. }
    21. }
    22.  
    [/INDENT]
    I'm trying to make the object's position to be between "Wheel1" and "BackSusTop", then i want the x-scale of the object be identical to the distance between "Wheel1" and "BackSusTop", and i want the objects rotation to point at both "Wheel1" and "BackSusTop".
    The positioning works just fine, but i can't get the rotation and scaling to work at the same time.
    If i removes the rotation in the script, the scaling works. But when i add the rotation, then scaling is suddenly ignored.
    Why can't it both scale and rotate?



    Thanks ind Advance :)
     
    Last edited: Dec 4, 2014
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Can you put your code inside [*code=CSharp][/code] (without "*"). Thanks.
     
  3. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    Translate the distance to a scale relative to the width of the object. For example, if the width of the object is 10 and the distance between "Wheel1" and "BackSusTop" is 20 then you need a scale of 2 or:

    Needed scale = target distance/ original distance
     
  4. Dytdyt

    Dytdyt

    Joined:
    Nov 9, 2014
    Posts:
    7
    Done :)

    RSG: I don't think that will work, because the entire scaling progress is being ignored. My current method works fine, as long i'm not trying to use the rotation :)
    But i can try giving it a shot later, when i'm home from work
     
  5. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I tried his solution:

    Code (CSharp):
    1. public class MySuperTest : MonoBehaviour {
    2.     public Transform go1;
    3.     public Transform go2;
    4.  
    5.     void Update() {
    6.         float distance = Vector3.Distance(go1.position, go2.position);
    7.  
    8.         transform.position = (go1.position + go2.position) / 2;
    9.         transform.LookAt(go1);
    10.         transform.localScale = new Vector3(0.3f, 0.3f, distance / 2);
    11.     }
    12. }
    mysupertest.jpg

    The script is attached to the cylinder. I can move my cubes, the cylinder is gonna follow them and with the right scale ;)
     
    Dytdyt likes this.
  6. Dytdyt

    Dytdyt

    Joined:
    Nov 9, 2014
    Posts:
    7
    Thanks guys!
    The solution was just to change "newVector3(list, 0.1f,1f)" to= "newVector3(0.1f, 0.1f,dis)".... Embarrassing mistake :D
     
  7. Kirills

    Kirills

    Joined:
    Apr 25, 2014
    Posts:
    11
    Dytdyt, how is your motocross game development ? Send me a message, I want to offer something. Thank you.
     
  8. diego_ger

    diego_ger

    Joined:
    Dec 17, 2017
    Posts:
    8
    Sorry for bringin back this, but I have a similar problem and I'm totally lost.
    I have an object that should look to a child of another gameobject (which parent rotate a small angle from one side to the other like a windshield wiper) and a the same time it should scale so it looks like it's conected to that parent object.
    The look part I got it with transform.LookAt(target); But the scaling thing can't make it work.
    The original scale of the object is (1,1,1). When the target moves, the distance between the pivot of the obect and the target changes. I'd like to use that variation for scaling the object on the z axis, but I don't know how.