- Home /
How would you get one variable to be directly correlated to another variable?
I am trying to figure out a way to cause the distance of ObjectA from it's origin position to directly control the angle of ObjectB. The player controls the position of ObjectA by clicking and dragging with the mouse and I have a hinge joint with max limit controlling the angle of ObjectB.
So for example, the default distance of ObjectA from it's origin is 0, so the angle of ObjectB stays at 0 degrees. But if the player drags ObjectA the max distance away from it's origin (it has a radius of 5), I need the max limit of ObjectB to become 90 degrees. But I also need all of the angles in between to be represented as the player moves through them. So when the player moves ObjectA to half of it's max distance (2.5 units away) the angle of ObjectB will be 45 degrees.
I am not really sure how to search for this cause I'm not sure what this effect would is called. I started looking into Lerp functionality because it seems to be able to represent gradations between two variables, but I'm not sure that it is what I am looking for since from what I have seen, it uses a time variable. Rather than having the value change over time, I need mine to change according to the player's actions. So even if someone doesn't have a direct answer for me, I would appreciate being pointed in the right direction just to get me started on what I need to search for. Thanks in advance for any help!
Answer by robertbu · Aug 21, 2014 at 04:33 AM
I believe this is what you are looking for:
var angle = Mathf.Lerp(minAngle, maxAngle, distance/maxDistance);
Yes, I think this is what I'm looking for! I didn't know that the third value didn't have to be a measurement of time. However I am still having a bit of trouble implementing it. I have tried a few different things which give me different errors. This is the code I have right now:
var $$anonymous$$Angle : float = 45;
var maxAngle : float = -45;
var distance : Vector3;
var maxDistance : Vector3;
var sqrObject : GameObject;
var launchObject : GameObject;
var sqrPos : Vector3;
var launchPos : Vector3;
var limits = hingeJoint.limits;
function Start()
{
sqrObject = gameObject.Find("squirrel");
launchObject = gameObject.Find("launchObject");
}
function Update()
{
sqrPos = sqrObject.transform.position;
launchPos = launchObject.transform.position;
distance = launchPos - sqrPos;
var angle = $$anonymous$$athf.Lerp($$anonymous$$Angle, maxAngle, distance);
limits.max = angle;
}
But for some reason this tells me that I can't use $$anonymous$$athf.Lerp with (float,float,float) even though I have distance as a Vector3.
Also I am not quite sure how to declare the maxDist using Vector3s, but when I put (,,distance/maxDistance) in there it tells me that "/" can't be used with a left hand and right hand side of type. I'm not really sure what that means.
You are mixing Vector3s and floats. Given what you have here:
$$anonymous$$ake 'maxDistance' a float at the top of the file. Be sure to assign it a value.
In the Lerp(), use 'distance.magnitude'.
Finally figured out how to put it all together. I was going about a lot of thing very, very wrong before, but it's working perfectly now. I'll post the working code below with comments for other people's future reference. Thanks again robertbu!
var $$anonymous$$Angle : float;
var maxAngle : float;
var sqrObject : Transform;
var launchObject : Transform;
function Update()
{
//Calculates the distance between both objects
var dist = Vector3.Distance(sqrObject.position, launchObject.position);
//Interpolates angle of object dependant on distance of two objects
var angle : float = $$anonymous$$athf.Lerp(maxAngle, $$anonymous$$Angle, dist);
//Changes angle of object by limiting hingeJoint max limit property
hingeJoint.limits.max = angle;
}
Note your code only works if you define the maximum distance as 1.0. That is any value outside the range of 0 to 1 for Lerp() is clamped to the 0 to 1 range.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161133im_/https://answers.unity.com/themes/thub/images/avi.jpg)