I have a character model that has a character controller and rigid body. I’m moving the character by charactercontroller.move, I was wondering how can i stop the character from moving. Here’s my code.
Code for moving the object:
void MoveTo() //function for moving
{
Vector3 dir = targetPosition - transform.position;
//Vector3 dir = new Vector3(targetPosition.x, targetPosition.y, targetPosition.z) - transform.position;
float dist = dir.magnitude;
float move = speed * Time.deltaTime;
if(dist > move)
{
cc.Move(dir.normalized * move);
//transform.position += dir.normalized * move; //>>OLD Code
}
else
{
cc.Move(dir);
//transform.position = targetPosition; //>>OLD Code
anim.SetFloat(hash.moveSpeedFloat, 0f); // sets the speed variable in the animator
}
//Movement
//transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime; //>>OLD Code
//Rotation Smooth
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotateSpeed);
}
Code for collision detection:
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.gameObject.tag == Tags.enemy) //if the character controller hits the enemy
{
anim.SetFloat(hash.moveSpeedFloat, 0f);
}
}
As you can see I’m just setting variables for the animation to stop in the OnControllerColliderHit function. I was wondering if there’s a way to stop the moving function when the collider hits the enemy.
Thank you in advance.