Got unpredictable results with the following script in C#
void FixedUpdate ()
{
//move to position endpoint
if (Input.GetKey ("z")) {
var pos = transform.position;
var endpoint = new Vector3 (5, 5, 5);
transform.position = endpoint - pos; }
//come back from position
if (Input.GetKey ("x")) {
var pos = transform.position;
var startpoint = new Vector3 (0, 0, 0);
transform.position = startpoint - pos; }
}
The code should move the object from whatever position it may be to endpoint when “z” is pressed (and stay there if z is pressed again) and move to startpoint if “x” is pressed (and stay there).
Problem_1:object at (0,0,0), keep pressed z, : object flickers very fast between origin and designated endpoint. Why is that? I would understand to flicker in place as FixedUpdate means once per frame the position is verified and if already at 5,5,5 the difference vector should be zero (no change in position).
Problem_2:object at (0,0,0), alternate pressing z and x: object gets to progresively further positions. Why? The vector difference should bring the object from where ever it may be to the designated positions.
Problem_3: continue some time alternating z and x and the object should get to positions theoretically unobtainable from the fixed coordinates. For example, how does it get to a transform x axis of 6 adding and subtracting zeros and fives? I would understand if it were a smooth movement, but in this case it’s move from A to B in one frame.
Problem_4:sometimes, with slightly larger code, pressing the keys does not produce any result (about one in 4 keystrokes gets the result). Why? It’s as if the action must accur between a specific interval inside each frame or be ignored. Long and hard keystrokes seem to improve the rate. Is there a “pressed” value to diminish to make the scrip more responsive?
That’s about it. Thanks.
PS. Hope this get’s posted