- Home /
My variable doesn't update real-time?
Hello,
when I update the variable distance in play mode it doesn't update in real-time?
#pragma strict
var Distance: float; //Value determind how much the object is away from the mouse.
private var hardX: float=Distance;
private var hardY: float=Distance;
private var diff =Vector3(hardX,hardY,0);
var followingObject : Transform; //The object that's gonna follow the mouse.
function Update ()
{
var v3T = Vector3(Input.mousePosition.x+diff.x, Input.mousePosition.y+diff.y, 106.6723);
v3T = Camera.main.ScreenToWorldPoint (v3T);
followingObject.position = v3T;
if (Input.GetKeyDown('space'))
{
Distance +=100;
}
}
Thanks.
Answer by AlucardJay · Feb 27, 2013 at 08:14 PM
You cannot do calculations outside of functions.
#pragma strict
var Distance: float; // Value determind how much the object is away from the mouse.
private var hardX : float;
private var hardY : float;
private var diff : Vector3;
var followingObject : Transform; // The object that's gonna follow the mouse.
function Update()
{
hardX = Distance;
hardY = Distance;
diff = Vector3( hardX,hardY,0 );
var v3T = Vector3( Input.mousePosition.x + diff.x, Input.mousePosition.y + diff.y, 106.6723 );
v3T = Camera.main.ScreenToWorldPoint( v3T );
followingObject.position = v3T;
if ( Input.GetKeyDown('space') )
{
Distance += 100;
}
}
Thanks, it worked, I moved them to the Update function. :)
Sorry, I overlooked distance being modified at the end of update, but yeah you get the idea. I shall fix the answer, Happy Coding =]
Answer by nventimiglia · Feb 27, 2013 at 07:51 PM
Set the transform. If you don't use Distance, Distance does not get used.
Thanks for the answer.
I already used a object for the transform, the problem, when I change the Distance in play mode i don't get any changes, I must change it in the actual script why ?
You are updating some magical "distance" variable. This variables does nothing. You need to set the transform of the local object if the local object is to move. gameObject.transform.position += gameObject.transform.forward * Distance.
Your answer
Follow this Question
Related Questions
How often is the update function called ? 1 Answer
Make events happen at once 0 Answers
Fixed timestep... 1 Answer
Is It Bad To Have Your Jump In Update()? 3 Answers