- Edited
I am rewriting my game in godot due to recent comments made by the unity ceo. I do not wish to give that man any money. So I am learning gdscript and I am hung on the differences between unity coroutine and gdscripts. Here is my coroutine I had in unity.
`private IEnumerator MovePlayer(Vector3 direction)
{
isMoving = true;
float elapsedTime = 0;
origPos = transform.position;
targetPos = origPos + direction;
while(elapsedTime < timeToMove)
{
transform.position = Vector3.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
elapsedTime += Time.deltaTime;
yield return null;
}
transform.position = targetPos;
isMoving = false;
}`
What this code is doing is lerping the players position to the next tile over in the tilemap. I start the coroutine with this code
`void Update()
{
if(Input.GetKey(KeyCode.W) && !isMoving)
{
StartCoroutine(MovePlayer(Vector3.up));
}
if(Input.GetKey(KeyCode.A) && !isMoving)
{
StartCoroutine(MovePlayer(Vector3.left));
}
if(Input.GetKey(KeyCode.S) && !isMoving)
{
StartCoroutine(MovePlayer(Vector3.down));
}
if(Input.GetKey(KeyCode.D) && !isMoving)
{
StartCoroutine(MovePlayer(Vector3.right));
}
}`
Now in gdscript this is what I have so far but it doesnt seem to be lerping the positions. This is the same move coroutine as above
`func move(delta):
ismoving = true
var elapsedTime = 0
last_position = position
target_position = last_position + movedirection
if elapsedTime < timetomove:
position = lerp(last_position, target_position, (elapsedTime / timetomove))
elapsedTime += delta
yield()
position = target_position
ismoving = false
`
This is the process function I have running for the coroutine
`func _process(delta):
if get_node("../Cam") != null:
tween = get_node("../Cam").tweenGetter()
if !tween:
if Input.is_action_pressed("ui_left") && !ismoving:
movedirection = Vector2(-1, 0)
elif Input.is_action_pressed("ui_right") && !ismoving:
movedirection = Vector2(1, 0)
elif Input.is_action_pressed("ui_up") && !ismoving:
movedirection = Vector2(0, -1)
elif Input.is_action_pressed("ui_down") && !ismoving:
movedirection = Vector2(0, 1)
else:
movedirection = Vector2.ZERO
var co = move(delta);
while co is GDScriptFunctionState && co.is_valid():
co = co.resume()
updateSprite()
if movedirection != Vector2.ZERO :
updateAnimation("Walk")
else:
updateAnimation("Idle")`
The player overall does move but does not lerp which is the issue. Maybe im not calling the coroutine correctly or I have a misunderstanding of them in gdscript. In unity I always considered them a async function. If someone could help out and explain to me what Im doing wrong that would be great.