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

Problems with Proximity AI Script

Discussion in 'Scripting' started by unity_F-Ht0AEJZbf9iA, Aug 5, 2020.

  1. unity_F-Ht0AEJZbf9iA

    unity_F-Ht0AEJZbf9iA

    Joined:
    Jul 29, 2020
    Posts:
    6
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Opponent : MonoBehaviour {
    7.  
    8. public class Ball;
    9. float range;
    10. float speed;
    11. Rigidbody rb;
    12.  
    13. void Start()
    14. {
    15. rb=GetComponent<Rigidbody>();
    16. }
    17.  
    18. void Update()
    19. {
    20.  
    21. range = Vector3.Distance(Ball.transform.position, transform.position);
    22.  
    23. if (range < 40)
    24. {
    25. transform.LookAt(Ball.transform.position);
    26. }
    27.  
    28. if (range < 30 && range > 15)
    29. {
    30. rb.AddForce(Vector3.forward*25*speed);
    31. }
    32. }
    33. }
    I need some help with this code, it is meant to be a script for an AI that will chase a ball. I have been working on it for a few weeks for a school project but I am still getting a compiler error telling me that I am missing { and } somewhere within the script. I have tried multiple positions and looked at other forums for help but I am still getting the error. Can anyone help me out or point out where I have gone wrong with the end brackets? Thanks
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Change this line
    Code (CSharp):
    1. public class Ball;
    to
    Code (CSharp):
    1. public GameObject Ball;
    The reason it's complaining about brackets is because it thinks you're trying to declare another class due to this error.
     
  3. unity_F-Ht0AEJZbf9iA

    unity_F-Ht0AEJZbf9iA

    Joined:
    Jul 29, 2020
    Posts:
    6
    Thank you very much it finally solved the problem, now all I have to do is identify the Opponent's Speed value. Thanks I've been stuck for like 2 weeks on this.