Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Help Needed AI Script Error

Discussion in 'Scripting' started by FirstBB8Droid, Jun 3, 2020.

  1. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I'm making an fps and I'm trying to add AI to the enemies and I'm making it right now so they move where I click. The thing is I'm getting this error that says

    Assets\enemyNavigation.cs(31,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

    Here's the code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class enemyNavigation : MonoBehaviour
    7. {
    8.  
    9.     protected NavMeshAgent Agent;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         Agent = GetComponent<NavMeshAgent>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.         Agent.updatePosition = false;
    22.         Agent.updateRotation = false;
    23.         Agent.updateUpAxis = false;
    24.         if (Input.GetButtonDown("test"))
    25.         {
    26.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.             RaycastHit hitinfo;
    28.             Physics.Raycast(ray, out hitinfo);
    29.             Agent.SetDestination(hitinfo.point);
    30.         }
    31.         transform.position +- Agent.desiredVelocity * Time.deltaTime;
    32.     }
    33. }
    34.  
    if you need anything else just ask, Thanks.
     
  2. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    should be
    Code (CSharp):
    1. transform.position += Agent.desiredVelocity * Time.deltaTime;
    to remove that error.

    If you want to get the enemies to move to that location setting up the navMeshAgent properly and giving it a destination should be all you need to do.
     
    FirstBB8Droid likes this.
  3. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    Thanks man this helps a lot
     
  4. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I'm finding that it still won't move even though I've tried everything to fix it, do you think it could be an error with my code or with my NavMeshAgent
     
  5. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    ne
    ver mind, I changed the input to move the enemy and that fixed it all. Thanks for your help.