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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Need help with Proximity AI script

Discussion in 'Scripting' started by unity_F-Ht0AEJZbf9iA, Jul 29, 2020.

  1. unity_F-Ht0AEJZbf9iA

    unity_F-Ht0AEJZbf9iA

    Joined:
    Jul 29, 2020
    Posts:
    6
    I'm trying to make a proximity AI script that will made an AI character chase a ball in a soccer game I am making for a school project. I've looked at multiple threads online and have asked for help from teachers and students.

    This is the script I have. I have done something to improve it but I am still getting compiler errors.

    public class GameObject Ball;
    float range;
    float speed;
    Rigidbody rb;

    void Start()
    {
    rb=GetComponent<Rigidbody>();
    }

    void Update()
    {
    range = Vector3.Distance(Ball.transform.position, transform.position);
    if (range < 40)
    {
    transform.LookAt(Ball.transform.position);
    }
    if (range < 30 && range > 15)
    {
    rb.AddForce(Vector3.forawrd*25*speed);
    }
    }

    These are the compiler errors that are popping up for me. I've tried multiple things to try and solve them but no matter what I do they still come up in the console log.

    error CS1022: Type or namespace definition, or end-of-file expected
    error CS0116: A namespace cannot directly contain members such as fields or methods
    error CS1514: { expected
    error CS1513: } expected

    I am using Unity 2018.4.15f1, any help or tips on how to solve the problem would be very much appreciated.
     
  2. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    707
    it does not appear you are posting the full script... also you can post it to show code to make it easier to read look at your options above while typing ..
     
  3. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Your class isn't defined properly.

    You did:

    Code (CSharp):
    1. public class
    2.     //your code here
    It should be

    Code (CSharp):
    1. public class ClassName {
    2.     //your code here
    3. }
    or if you intend to place this script on a GameObject, the class should inherit from Monobehaviour

    Code (CSharp):
    1. public class ClassName : MonoBehaviour {
    2.     //your code here
    3. }
    Make sure the ClassName matches the file name. Case is important.

    Also you'll need to correct the spelling of forward on this line.

    Code (CSharp):
    1. rb.AddForce(Vector3.forawrd*25*speed);
    Please use code tags when you post code, it makes it easier to read :)
     
  4. unity_F-Ht0AEJZbf9iA

    unity_F-Ht0AEJZbf9iA

    Joined:
    Jul 29, 2020
    Posts:
    6
    Thanks for the replies everyone sorry for not using the code tag thing I am a bit noobish