Home > Software design >  Check if the goal has been achieved
Check if the goal has been achieved

Time:08-10

I have a character and a target where it should go. If the target is in other coordinates, then the character moves towards it, when he reach it, then the walk animation is turned off.

I have 2 problems:

  1. When the target is moved and the character reaches it, the coordinates of the target and the character are a little different.

  2. Target (GameObject plane) after moving has strange Y-meanings. How to make Y always equal to 0.

The screenshot shows that the character has reached the goal, but the animation does not end because their coordinates are slightly different.

Screenshot

Move

transform.rotation = Target.transform.rotation;
var direction = (Target.transform.position - transform.position).normalized;
_characterController.Move(direction * WalkSpeed * Time.deltaTime);

Target

if (DistanceToPlayer >= MaxDistanceToPlayer)
{
    if(isMove == false)
    {
        isMove = true;
        Vector3 NewPositionToPlayer = Player.transform.position   new Vector3 (Random.Range(-5.0f, 5.0f), 0f, Random.Range(-5.0f, 5.0f));
        Target.transform.position = NewPositionToPlayer;
        
        animator.SetBool("isWalking", true);
    }
}

if (DistanceToPlayerPivot >= MaxDistanceToPlayer)
{
    isMove = false;
}

if(RelativePosition == Target.position)
{
    animator.SetBool("isWalking", false);
    isMove = false;
}

CodePudding user response:

Instead of comparing RelativePosition to Target.position compare RelativePosition to a small area surrounding Target.position - use the size of the target to determine the size of the area.

Comparisons:

  1. If the x-axis of RelativePosition is greater than the x-axis of (Target.position - (width of the target / 2)) AND the x-axis of RelativePosition is less than the x-axis of (Target.position (width of the target / 2))

  2. If the y-axis of RelativePosition is greater than the y-axis of (Target.position - (height of the target / 2)) AND the y-axis of RelativePosition is less than the y-axis of (Target.position (height of the target / 2))

If you did both of those comparisons and they were both true then the character should be within the target.

  • Related