Question by
patatra · Mar 19 at 08:25 AM ·
script.coroutinepathfindingcharacter controllermove an object
Can you use a character controller in a coroutine?
hye everyone,
I'm making a pathfinfing astar where the movement is done with the mouse click. I'm using a character controller for the movement because I need one that can climb stairs and steps. The pathfind works very well with a "Vector3.MoveTowards" but when I use a character controller the movement becomes anarchic.
IEnumerator FollowPath()
{
Debug.Log("->FollowPath of Basehero");
BaseTile currentTile = _paths[0].Neighbor;
while (true)
{
Vector3 pos = transform.position;
pos.y = 0;
if (Vector3.Distance(pos, currentTile.PosPath) <= 0.05f)
{
transform.position = currentTile.PosPath;
targetIndex++;
if (targetIndex >= _paths.Length)
{
yield break;
}
currentTile = _paths[targetIndex].Neighbor;
}
//character controller
_motor.MoveToPoint(_paths[targetIndex].Direction);
yield return null;
}
}
Function for the charatcer controler in the _motor class
public void MoveToPoint(Vector3 direction)
{
Debug.Log("move : " + direction);
//_isGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, Groundmask);
//temp
_isGrounded = true;
if (_isGrounded && _velocity.y < 0)
{
_velocity.y = -2f;
}
_controller.Move(direction * Speed * Time.deltaTime);
_velocity.y += Gravity * Time.deltaTime;
_controller.Move(_velocity * Time.deltaTime);
}
Good Day,
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613054354im_/https://answers.unity.com/themes/thub/images/avi.jpg)