- Home /
Object lerping at start
My script allows me, when I hit a certain point, to lerp to a certain position. At the start the object lerps to -15 on the x value which should only happen when the object is less then -17 on the x value which it is not. If anyone can help it would be much appreciated.
//Original Object
var object : GameObject;
//Object position + end poition modified
var objectPos : Vector3;
var objectEnd : Vector3;
//Controllers
var duration : float = 1.0;
var startTime : float;
//Barriers
var topPos : float = 1;
var rightPos : float = 5;
var leftPos : float = -12;
var bottomPos : float = -17;
function Start () {
startTime = Time.time;
objectPos = object.transform.position;
objectEnd = object.transform.position;
}
function Update () {
if (object.transform.position.x >= rightPos) {
objectEnd.x = 4;
object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time - startTime) / duration);
}
if (object.transform.position.x <= leftPos) {
objectEnd.x = -10;
object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time - startTime) / duration);
}
if (object.transform.position.y <= topPos) {
objectEnd.y = -1;
object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time - startTime) / duration);
}
if (object.transform.position.x <= bottomPos) {
objectEnd.x = -15;
object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time - startTime) / duration);
}
}
I don't see anything wrong with the line that's evaluating x to LE bottomPos; are there other scripts that might be applying force/gravity/movement onto this GO?
Nope, just the one. I have even isolated it so its the only thing running, and without mouse clicks, it moves like it should but without purpose.
Answer by robertbu · Aug 13, 2013 at 06:33 AM
The most likely cause of your specific issue is a value different in the inspector than the value you have in the code. By default in Javascript class variables are public, so topPos, rightPos, leftPos and bottomPos are all public and can be viewed in the inspector. The values you set in code for these variables are only used once when the component is attached. Changes to these values in the script does not change the values you see in the inspector. If this is your problem, you can do a couple of things. First, you can just set the values you want in the inspector, second, you can make the variables private, or third you can reset the values. To reset the values, select the game object in the hierarchy, in the inspector on the right side of your script component, click on the gear drowdown and select 'Reset'.
I can't thank you enough, robertbu. Resetting the values helped.