0
votes

I've been struggling with the movement of my player since its movement was based on updating its world position with time frames. Using Update or FixedUpdate didn't change anything.

Since, I setted up another movement which involved a lot of changes in my code, this time based on speed (* Time.delaTime) and Update make my player moving faster when the mouse button is kept down, but FixedUpdate fix the problem and whatever if I just clic or keep the mouse down my player keep the same speed.

Code:

    void FixedUpdate()
    {
        if(Input.GetMouseButton(0))
        {
            Vector3 target = GetMouseWorldPosition();
            transform.position = Vector3.MoveTowards(transform.position, target , speed * Time.deltaTime);

Good luck and have fun :)

1

1 Answers

1
votes

Issue 1

As you are already using Coroutine, you can use yield return new WaitForSeconds (1) instead of yield return null. you can adjust the number to set the wait duration between steps. Don't purposely slow down your CPU to achieve anything about timing. And don't mess up with Fixed Update time it's for physics simulation.

Issue 2

change it to yield return new WaitForSeconds (Input.GetMouseButton(0)?.5f:1);, so it waits shorter time if mouse button is held.

Issue 3

if(pathfinding==null || grid=null){
...(do random movement)
}else{
...(do pathfinding movement)
}

EDIT 1

if you don't want teleporting, you need to write transition yourself.

Vector3 oldPos = transform.position;
Vector3 newPos = nodeWaypoint;
float time = 0;
while(time<1){
    yield return null;
    time +=Time.deltaTime;
    transform.position = Vector3.Lerp(oldPos,newPos,time);
}

Or just use DoTween

but TBH, your coding is quite problematic. it will keep doing pathfinding and calling UpdatePosition when the player holding click.