LookAt and scale

Hi guys

My code is this:

[INDENT]usingUnityEngine;
usingSystem.Collections;

publicclassSuspensionModelling : MonoBehaviour {

public float dist;

//Usethisforinitialization
void Start () {

}

//Updateiscalledonceperframe
voidUpdate () {
dist = Vector3.Distance (GameObject.Find ("Wheel1").transform.position, GameObject.Find ("BackSusTop").transform.position);
gameObject.transform.position = (((GameObject.Find ("Wheel1").transform.position)+(GameObject.Find ("BackSusTop").transform.position))/2);
gameObject.transform.LookAt (GameObject.Find ("BackSusTop").transform.position); // TRANSFORM
gameObject.transform.localScale = newVector3(dist, 0.1f,1f); // SCALE
}
}

[/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 :slight_smile:

Can you put your code inside [code=CSharp][/code] (without ""). Thanks.

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

Done :slight_smile:

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 :slight_smile:
But i can try giving it a shot later, when i’m home from work

I tried his solution:

public class MySuperTest : MonoBehaviour {
    public Transform go1;
    public Transform go2;

    void Update() {
        float distance = Vector3.Distance(go1.position, go2.position);

        transform.position = (go1.position + go2.position) / 2;
        transform.LookAt(go1);
        transform.localScale = new Vector3(0.3f, 0.3f, distance / 2);
    }
}

1873014--120372--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 :wink:

1 Like

Thanks guys!
The solution was just to change “newVector3(list, 0.1f,1f)” to= “newVector3(0.1f, 0.1f,dis)”… Embarrassing mistake :smile:

Dytdyt, how is your motocross game development ? Send me a message, I want to offer something. Thank you.

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.