I was trying to make enemies walk and follow the player, but there is an error

using UnityEngine;
using UnityEngine.AI;

public class OpponentFollowing : MonoBehaviour
{
    public NavMeshAgent agent;

    public Transform player;

    public LayerMask whatIsground, whatIsPlayer;

    //Patroling
    public Vector3 walkPoint;
    bool walkPointSet;
    public float walkpointRange;

    //Attacking
    public float timeBetweenAttacks;
    bool alreadyAttacked;

    //States
    public float sightRange, attackRange;
    public bool playerInSightRange, playerInAttackRange;

    private void Awake()
    {
        player = GameObject.Find("Player").transform;
        agent = GetComponent<NavMeshAgent>();
    }
    private void Update()
    {
        playerInSightRange = Physics.CheckCapsule(transform.position, sightRange, whatIsPlayer);
        playerInAttackRange = Physics.CheckCapsule(transform.position, attackRange, whatIsPlayer);

        if (!playerInSightRange && !playerInAttackRange) Patroling();
        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
        if (playerInSightRange && playerInAttackRange) AttackPlayer();
    }
    private void Patroling()
    {
        if (!walkPointSet) SearchWalkPoint();

        if (walkPointSet)
        {
            agent.SetDestination(walkPoint);
        }
        Vector3 distanceToWalkPoint = transform.position - walkPoint;

        if (distanceToWalkPoint.magnitude < 1f)
        {
            walkPointSet = false;
        }
    }
    private void SearchWalkPoint()
    {
        Vector3 float randomX = Random.Range(-walkpointRange, walkpointRange);
        Vector3 float randomZ = Random.Range(-walkpointRange, walkpointRange);

        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);

        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsground))
        {
            walkPointSet = true;
        }
    }
    private void ChasePlayer()
    {
        agent.SetDestination(player.position);
    }
    private void AttackPlayer()
    {
        agent.SetDestination(player.position);

        transform.LookAt(player);

        if (!alreadyAttacked)
        {
            alreadyAttacked = true;
            Invoke(nameof(ResetAttack), timeBetweenAttacks);
        }
    }
    private void ResetAttack()
    {
        alreadyAttacked = false;
    }
}

these are the errors
pls help

Assets\OpponentFollowing.cs(56,17): error CS1002: ; expected
Assets\OpponentFollowing.cs(57,17): error CS1002: ; expected

Looks like you have two types declared on lines 56 and 57.

Vector3 or float, pick one :slight_smile:

You can mouse over any function name in Visual Studio and the first type shown in the pop up is what the function returns. In the case of Random.Range(), that’s a float. So you want these to be floats not Vector3’s.

ok.so i got rid of vector3
and now it displays

Assets\OpponentFollowing.cs(32,71): error CS1503: Argument 2: cannot convert from ‘float’ to ‘UnityEngine.Vector3’
Assets\OpponentFollowing.cs(32,72): error CS1503: Argument 2: cannot convert from ‘float’ to ‘UnityEngine.Vector3’

i really need help with this

Your CheckCapsule function on line 32 and 33 should take a Vector3 for start, a Vector3 for end, a float for Radius and finally a LayerMask. You can see the arguments the function needs by pressing ctrl+shift+space with the caret over the arguments and navigation the overloads using the up/down arrow keys.

Looks like you’re missing a Vector3 somewhere. It’s the either the ‘start’ argument or the ‘end’ argument. Whichever one the transform.position you’ve already got doesn’t represent.

may you please tell me where i have to put the Vector3?
I’m so confused,

The functions on 32 and 33 takes two vector 3’s. You only sent it one :slight_smile:

        playerInSightRange = Physics.CheckCapsule(transform.position, sightRange, whatIsPlayer);
        playerInAttackRange = Physics.CheckCapsule(transform.position, attackRange, whatIsPlayer);

Should be

        playerInSightRange = Physics.CheckCapsule(transform.position, AnotherVector3Here, sightRange, whatIsPlayer);
        playerInAttackRange = Physics.CheckCapsule(transform.position, AnotherVector3Here, attackRange, whatIsPlayer);

THANK YOU SO MUCH
IT WORKSS
HALLELUJAH

1 Like

Congrats :slight_smile: