Point and Click (yield WaitForFixedUpdate question)

Hi, I’m trying to make a point and click interface (trying to build a prototype for a more modern 2.5D-ish point and click adventure game).

I’m not much of a coder and there are probably all kinds of things wrong with my code so far, but I’ll figure them out.

Here is my code :

function Awake(){
	var moveDirectionc = Vector3.zero;
	var targetPoint = Vector3.zero;
}

var speed = 0.35;

function Update () {

	if (Input.GetMouseButtonDown(0)){
		moveaa();
	}
}

function moveaa() {
	var screenSpace = Camera.main.WorldToScreenPoint(transform.position); 
	var controller : CharacterController = GetComponent(CharacterController);
	var transformx = this.transform.position.x;
	var targetPoint = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,screenSpace.y,screenSpace.z)); 
	moveDirectionc = Vector3(targetPoint.x - transformx,0,0);
	moveDirectionc = Vector3.Normalize(moveDirectionc)*speed/100;
	moveDirectionc.y = -1;
		
	while (Mathf.Abs(transformx - targetPoint.x) > 0.02){
		controller.Move(moveDirectionc);
		transformx = this.transform.position.x;
		yield WaitForFixedUpdate; 

		if (Input.GetMouseButtonDown(0)){
			break;
		}
	}
}

The code works (I only care about moving the main character on the X axis). The only problem is, it’s not framerate independent. On an older computer were the framerate is somewhat choppy, the main character moves really slowly.

And here is my question: shouldn’t yield WaitForFixedUpdate; take care of that? Doesn’t this line make the while loop repeat at the fixed rate?

You don’t need the yield for this. Just multiply the movement vector by Time.deltaTime and call the movement function each frame update (it’s actually best to call it from FixedUpdate rather than Update).

If I remove the while loop, the main character just moves a little bit when I click and that’s it.

I need to actually keep moving towards the place I clicked until it reaches close enough to it, or there is another click elsewhere.

And maybe my approach is faulty, but still, why doesn’t yield WaitForFixedUpdate work properly? It’s supposed to make the while loop repeat at the fixed rate, right?

You’re calling it (indirectly) from the Update function. I don’t think you’re supposed to do that. Try taking the entire while loop out of that function and putting it into a coroutine. Then start that coroutine from MoveAA instead. I think that’s the correct way to do things.