Search Unity

nothing special

Discussion in 'Scripting' started by RealPpTheBest, Jan 27, 2019.

  1. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    AI Script.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class AI : MonoBehaviour
    8. {
    9.     /*Animator
    10.     Animator controller;
    11.     //controller.SetBool("isChasing", true);*/
    12.    
    13.     //AI sight
    14.     public bool playerIsInLOS = false;
    15.     public float fieldOfViewAngle = 160f;
    16.     public float losRadius = 45f;
    17.    
    18.     //AI sight and memory
    19.     private bool aiMemorizesPlayer = false;
    20.     public float memoryStartTime = 10f;
    21.     private float increasingMemoryTime;
    22.    
    23.     //AI hearing
    24.     Vector3 noisePosition;
    25.     private bool aiHeardPlayer = false;
    26.     public float noiseTravelDistance = 50f;
    27.     public float spinSpeed = 3f;
    28.     public bool canSpin = false;
    29.     public float isSpinningTime; //search at player noise position
    30.     public float spinTime = 3f;
    31.    
    32.     //Patrolling randomly between waypoints
    33.     public Transform[] moveSpots;
    34.     private int randomSpot;
    35.    
    36.     //Wait time at waypoint for patrolling
    37.     private float waitTime;
    38.     public float startWaitTime = 1f;
    39.    
    40.     //AI strafe
    41.     public float disToPlayer = 5.0f; //straferadius
    42.    
    43.     private float randomStrafeStartTime;
    44.     private float waitStrafeTime;
    45.     public float t_minStrafe;
    46.     public float t_maxStrafe;
    47.    
    48.     public Transform strafeRight;
    49.     public Transform strafeLeft;
    50.     private int randomStrafeDir;
    51.    
    52.     //When to chase
    53.     public float chaseRadius = 20f;
    54.    
    55.     public float facePlayerfactor = 20f;
    56.    
    57.     private void Awake()
    58.     {
    59.         nav = GetComponent<NavMeshAgent>();
    60.         //controller = GetComponentInParent<Animator>();
    61.         nav.enabled = true;
    62.     }
    63.    
    64.     //Use this for initialization
    65.     void Start()
    66.     {
    67.         waitTime = startWaitTime;
    68.         randomSpot = Random.Range(0, moveSpots.Length);
    69.         randomStrafeDir = Random.Range(0, 2);
    70.     }
    71.    
    72.     //Update is called once per frame
    73.     void Update()
    74.     {
    75.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    76.         if (distance <= losRadius)
    77.         {
    78.             CheckLOS();
    79.         }
    80.     }
    81.    
    82.     if (nav.isActiveAndEnabled)
    83.     {
    84.         if (playerIsInLOS == false && aiMemorizesPlayer == false && aiHeardPlayer == false)
    85.         {
    86.             Patrol();
    87.             NoiseCheck();
    88.            
    89.             StopCoroutine(AiMemory());
    90.         }
    91.        
    92.         else if (aiHeardPlayer == true && playerIsInLOS == false && aiMemorizesPlayer == false)
    93.         {
    94.             canSpin = true;
    95.             GoToNoisePosition();
    96.         }
    97.        
    98.         else if (playerIsInLOS == true)
    99.         {
    100.             aiMemorizesPlayer = true;
    101.            
    102.             FacePlayer();
    103.            
    104.             ChasePlayer();
    105.         }
    106.        
    107.         else if (aiMemorizesPlayer == true && playerIsInLOS == false)
    108.         {
    109.             ChasePlayer():
    110.            
    111.             StartCoroutine(AiMemory());
    112.         }
    113.     }
    114.    
    115.     //Hearing
    116.     void NoiseCheck()
    117.     {
    118.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    119.        
    120.         if (distance <= noiseTravelDistance)
    121.         {
    122.             if (SawSoundController.OnCollisionEnter())
    123.             {
    124.                 noisePosition = PlayerMove.playerPos;
    125.                
    126.                 aiHeardPlayer = true;
    127.             }
    128.             else
    129.             {
    130.                 aiHeardPlayer = false;
    131.                
    132.                 canSpin = false;
    133.             }
    134.         }
    135.     {
    136.     }
    137.    
    138.     //Go to noise position
    139.     void GoToNoisePosition()
    140.         nav.setDestination(noisePosition);
    141.        
    142.         if (Vector3.Distance(transform.position, noisePosition) <= 5f && canSpin == true)
    143.         {
    144.             isSpinningTime += Time.deltaTime;
    145.            
    146.             transform.Rotate(Vector3.up * spinSpeed, Space.World);
    147.            
    148.             if (isSpinningTime >= spinTime)
    149.             {
    150.                 canSpin = false;
    151.                 aiHeardPlayer = false;
    152.                 isSpinningTime = 0f;
    153.             }
    154.         }
    155.     }
    156.    
    157.     //Memory
    158.     IEnumerator AiMemory()
    159.     {
    160.         increasingMemoryTime = 0;
    161.        
    162.         while (increasingMemoryTime < memoryStartTime)
    163.         {
    164.             increasingMemoryTime += Time.deltaTime;
    165.             aiMemorizesPlayer = true;
    166.             yield return null;
    167.         }
    168.        
    169.         aiHeardPlayer = false;
    170.         aiMemorizesPlayer = false;
    171.     }
    172.    
    173.     //Line of sight
    174.     void CheckLOS()
    175.     {
    176.         Vector3 direction = PlayerMove.playerPos - transform.position;
    177.        
    178.         float angle = Vector3.Angle(direction, transform.forward);
    179.        
    180.         if (angle < fieldOfViewAngle * 0.5f)
    181.         {
    182.             RaycastHit hit;
    183.            
    184.             if (Physics.Raycast(transform.position, direction.normalized, out hit, losRadius))
    185.             {
    186.                 if (hit.collider.tag == "Player")
    187.                    
    188.                 {
    189.                     playerIsInLOS = true;
    190.                    
    191.                     aiMemorizesPlayer = true;
    192.                 }
    193.                 else
    194.                 {
    195.                     playerIsInLOS = false;
    196.                 }
    197.             }
    198.         }
    199.     }
    200.    
    201.     //Patrol
    202.     void Patrol()
    203.     {
    204.         nav.setDestination(moveSpots[randomSpot].position);
    205.        
    206.         if (Vector3.Distance(transform.position, moveSpots[randomSpot].position)
    207.         {
    208.             if (waitTime <=0)
    209.             {
    210.                 randomSpot = Random.Range(0, moveSpots.Length);
    211.                
    212.                 waitTime = startWaitTime;
    213.             }
    214.             else
    215.             {
    216.                 waitTime -= Time.deltaTime;
    217.             }
    218.         }
    219.     }
    220.    
    221.     //Chase player
    222.     void ChasePlayer()
    223.     {
    224.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    225.        
    226.         if (distance <= chaseRadius && distance > distToPlayer)
    227.         {
    228.             nav.SetDestination(PlayerMove.playerPos);
    229.         }
    230.        
    231.         else if (nav.isActiveAndEnabled && distance <= distToPlayer)
    232.         {
    233.             randomStrafeDir = Random.Range(0, 2);
    234.             randomStrafeStartTime = Random.Range(t_minStrafe, t_maxStrafe);
    235.            
    236.             if (waitStrafeTime <= 0)
    237.             {
    238.                 if (randomStrafeDir == 0)
    239.                 {
    240.                     nav.SetDestination(strafeLeft.position);
    241.                 }
    242.                
    243.                 else if (randomStrafeDir == 1)
    244.                 {
    245.                     nav.SetDestination(strafeRight.position);
    246.                 }
    247.                 waitStrafeTime = randomStrafeStartTime;
    248.             }
    249.             else
    250.             {
    251.                 waitStrafeTime -= Time.deltaTime;
    252.             }
    253.         }
    254.     }
    255.    
    256.     //Face player
    257.     void FacePlayer()
    258.     {
    259.         Vector3 direction = (PlayerMove.playerPos - transform.position).normalized;
    260.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    261.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * facePlayerFactor);
    262.     }
    263. }
    PlayerMove Script
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class PlayerMove : MonoBehaviour
    7. {
    8.     //Variables
    9.     public static Vector3 playerPos;
    10.    
    11.     //Just for Movement
    12.     public float speed = 400f;
    13.    
    14.     private Rigidbody rb;
    15.    
    16.     //For facing the mouse
    17.     private Camera mainCamera;
    18.    
    19.     //Use this for initialization
    20.     void Start()
    21.     {
    22.         rb= GetComponent<Rigidbody>();
    23.        
    24.         mainCamera = FindObjectOfType<Camera>();
    25.        
    26.         StartCoroutine(TrackPlayer());
    27.     }
    28.    
    29.     IEnumerator TrackPlayer()
    30.     {
    31.         while (true)
    32.         {
    33.             playerPos = gameObject.transform.position;
    34.             yield return null;
    35.         }
    36.     }
    37.    
    38.     //Update is called once per frame
    39.     void FixedUpdate()
    40.     {
    41.         //Movement Start
    42.         float moveHorizontal = Input.GetAxisRaw("Horizontal");
    43.         float moveVertical = Input.GetAxisRaw("Vertical");
    44.        
    45.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    46.        
    47.         rb.AddForce(movement * speed);
    48.         //Movement End
    49.        
    50.         //Face Mouse Start
    51.         Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
    52.         Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    53.         float rayLength;
    54.        
    55.         if (groundPlane.Raycast(cameraRay, out rayLength))
    56.         {
    57.             Vector3 pointToLook = cameraRay.GetPoint(rayLength);
    58.             Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
    59.            
    60.             transform.LookAr(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
    61.         }
    62.         //Face Mouse End
    63.     }
    64. }
    65.  
    SawSoundController
    Code (CSharp):
    1.  
    2. //Script not written by me.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. //Add this Script Directly to The Death Zone
    9.  public class SawSoundController : MonoBehaviour
    10.  {
    11.     public AudioClip saw;    // Add your Audi Clip Here;
    12.     // This Will Configure the  AudioSource Component;
    13.     // MAke Sure You added AudioSouce to death Zone;
    14.     void Start ()  
    15.     {
    16.         GetComponent<AudioSource> ().playOnAwake = false;
    17.         GetComponent<AudioSource> ().clip = saw;
    18.     }      
    19.  
    20.     void OnCollisionEnter ()  //Plays Sound Whenever collision detected
    21.     {
    22.         GetComponent<AudioSource> ().Play ();
    23.     }
    24.           // Make sure that deathzone has a collider, box, or mesh.. ect..,
    25.           // Make sure to turn "off" collider trigger for your deathzone Area;
    26.           // Make sure That anything that collides into deathzone, is rigidbody;
    27.  }
    28.  
    So, these are the 3 scripts. Now the problem I am facing are these errors..

    Assets/AI.cs(84,3): error CS1519: Unexpected symbol `if' in class, struct, or interface member declaration
    Assets/AI.cs(84,5): error CS1644: Feature `tuples' cannot be used because it is not part of the C# 4.0 language specification
    Assets/AI.cs(84,5): error CS8124: Tuple must contain at least two elements
    Assets/AI.cs(85,2): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
    Assets/AI.cs(85,2): error CS9010: Primary constructor body is not allowed
    Assets/AI.cs(111,16): error CS1525: Unexpected symbol `:', expecting `;' or `}'
    Assets/AI.cs(141,2): error CS1547: Keyword `void' cannot be used in this context
    Assets/AI.cs(141,23): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
    Assets/AI.cs(209,2): error CS1525: Unexpected symbol `{'

    I am not really new to Unity and C# (UnityScript), but I am new to 3d games, and this game is something like Granny by Dvloper, or Eyes by Paulina, or Dark Internet. So this AI script is really simple, but I dont know why but I can't fix the errors above, there were even more, but I could fix them. Please help me, I tried my best fixing them, but I couldn't. Plus I am 12, so I don't have so much expierence in C# ( I have 4 months of exp) but rather in JS ( 2 years ) and Java ( 8 months ).

    Plus the last script "SawSoundController" is not written by me.

    Any help is appreciated. Please don't comment on my english, if it is bad #NotEnglish
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    The first error points to the fact that the if statement is outside of any function/method, showing that you are new to all of this.
    also, C# is not UnityScript, and UnityScript isn't a thing anymore.

    I'd suggest you following more basic programming tutorials before delving into the realm of video games.
     
  3. visionman50

    visionman50

    Joined:
    Dec 19, 2017
    Posts:
    40
    hi. can you or some one else help with my project. using unity 2018.2.3 and my screen reader jaws 2019 tells me gui.utility process: 32 bit and got a 64 bit, also in my visual studio 2017 community project tied to unity, says some thing about when i try to build, end of line or sintax error. can some one contact me via e-mail and help me out and if they can help. that would be great. thanks. or maybe the log file. thanks. ps: been trying to fix this one error in my unity, for a few weeks, for a school assignment. not to do it for me, but to help me how to fix this. maybe trying to install a 32 bit into 64 bit and have got android studio, but could not seem to add it to the external tools and the for java, there. any help. in australia, if any one here on the forum from australia, or new zealand, able to help me out.jdk fk
     
  4. visionman50

    visionman50

    Joined:
    Dec 19, 2017
    Posts:
    40
    hi, got a unity error says gui.utility.process: 32 bit int and got unity 2018.2.3 64 bit, don't know why. if any one is in australia or new zealand, able to help. and also in the visual studio project, one error, says some thing about end of line or sintax. doing this for a school assignment. not to do it for me, but to help me to get it to build. been trying for weeks. driving me nuts. and maybe trying to install a 32 bit application into 64 bit. tried googling. do have android studio and the jdk but could not seem to attach that to the external editor, as totally blind and my screen reader jaws, able to not able to get to add the external tools, or editor. thanks.
     
  5. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi Sir, as I stated I am not new to Unity, and OK maybe I am weak at C#, but I am still learning. My first game was 2d and was made with JavaScript, because I am good at it. I have also used Java for many projects (Not in Unity) as I know it too, but C#, well I have been learning it for past 3 months, and when I was developing my first game, I used it for small things because I found out it to be more comfortable, but now as I am moving to a 3D game, I am using C# for my project and there were many errors but I fixed 80% of them.

    I have learned all 3 languages by watching Youtube, i watch channels like Brackeys, Unity3dCollege, etc.. Plus it would be much more helpful if someone helps me, I am not even asking for a script, just wanted help in a new language, which seems more comfortable. Please help, that would be nice. Plus, thanks for telling that UnityScript is not C# and UnityScript isn't a thing anymore, atleast learned something new. Once again, I am not asking for a script but rather asking for help to fix errors in my script.
     
  6. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    I can't figure out what you're saying, just a request can you fix script? Or atleast help me? If not, that's perfectly fine, then I guess I have to work on some other project, i guess..

    Plus can you tell, is my AI script good? I wrote it on my own..
     
  7. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    For your first issue //Assets/AI.cs(84,3): error CS1519: Unexpected symbol `if' in class, struct, or interface member declaration

    it's because you've put your if statements outside of a method. Your method Update closes

    Code (CSharp):
    1. void Update()
    2.     {
    3.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    4.         if (distance <= losRadius)
    5.         {
    6.             CheckLOS();
    7.         }
    8.     } << -- Update method ends here
    9.     if (nav.isActiveAndEnabled) << -- this if statement is not in a method
    10.     {
    Putting that in a method should fix a few errors.

    but then you have this error: "Assets/AI.cs(111,16): error CS1525: Unexpected symbol `:', expecting `;' or `}'"

    That is because

    Code (CSharp):
    1.       else if (aiMemorizesPlayer == true && playerIsInLOS == false)
    2.         {
    3.             ChasePlayer(): <-- needs to be a semiColon ; ChasePlayer();
    4.  
    5.             StartCoroutine(AiMemory());
    6.         }
    7.     }
    Then you have this error: "Assets/AI.cs(141,2): error CS1547: Keyword `void' cannot be used in this context"

    That is because

    Code (CSharp):
    1.     void NoiseCheck()
    2.     {
    3.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    4.  
    5.         if (distance <= noiseTravelDistance)
    6.         {
    7.             if (SawSoundController.OnCollisionEnter())
    8.             {
    9.                 noisePosition = PlayerMove.playerPos;
    10.      
    11.                 aiHeardPlayer = true;
    12.             }
    13.             else
    14.             {
    15.                 aiHeardPlayer = false;
    16.      
    17.                 canSpin = false;
    18.             }
    19.         }
    20.     { < ---
    21.     }
    the bracket is there

    Then this error : "Assets/AI.cs(141,23): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='"

    That is because

    Code (CSharp):
    1.   void GoToNoisePosition() < -- You have no opening bracket "{"
    2.         nav.setDestination(noisePosition);
    3.  
    4.         if (Vector3.Distance(transform.position, noisePosition) <= 5f && canSpin == true)
    The last one is a pain, im tired, going to bed now. "Assets/AI.cs(209,2): error CS1525: Unexpected symbol `{'. but fix your other errors and then look for similar typo issues.

    Those error messages are really good, it tells you the script name, the line where the error is and the reason. Theyre all syntax errors, which are mistakes the are really easy to make
     
    Last edited: Jan 28, 2019
    RealPpTheBest likes this.
  8. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    I can't believe I made such silly mistakes, it's just like my math's exam sheet, where I miss a number and lose my marks...

    Thank you so much! You just made me not quit a project I started. I appreciate your help and I will try to make less typos and improve my knowledge of C#, thanks once again!
     
    Last edited: Jan 29, 2019
  9. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi! @DeeJayVee, I am only left with two errors....

    Code (CSharp):
    1. //Script written by Pratyush Priyadarshi.
    2. //Script written in C# on 24-01-2019 at 14:45 IST and finished on 25-01-2019 at 16:40 IST.
    3.  
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7. using UnityEngine.AI;
    8.  
    9. public class AI : MonoBehaviour
    10. {
    11.     /*//Animator
    12.     Animator controller;
    13.     //controller.SetBool("isChasing", true);*/
    14.    
    15.     //NavMesh agent
    16.     public NavMeshAgent nav;
    17.    
    18.     //AI sight
    19.     public bool playerIsInLOS = false;
    20.     public float fieldOfViewAngle = 160f;
    21.     public float losRadius = 45f;
    22.    
    23.     //AI sight and memory
    24.     private bool aiMemorizesPlayer = false;
    25.     public float memoryStartTime = 10f;
    26.     private float increasingMemoryTime;
    27.    
    28.     //AI hearing
    29.     Vector3 noisePosition;
    30.     private bool aiHeardPlayer = false;
    31.     public float noiseTravelDistance = 50f;
    32.     public float spinSpeed = 3f;
    33.     public bool canSpin = false;
    34.     public float isSpinningTime; //search at player noise position
    35.     public float spinTime = 3f;
    36.    
    37.     //Patrolling randomly between waypoints
    38.     public Transform[] moveSpots;
    39.     private int randomSpot;
    40.    
    41.     //Wait time at waypoint for patrolling
    42.     private float waitTime;
    43.     public float startWaitTime = 1f;
    44.    
    45.     //AI strafe
    46.     public float distToPlayer = 5.0f; //straferadius
    47.    
    48.     private float randomStrafeStartTime;
    49.     private float waitStrafeTime;
    50.     public float t_minStrafe;
    51.     public float t_maxStrafe;
    52.    
    53.     public Transform strafeRight;
    54.     public Transform strafeLeft;
    55.     private int randomStrafeDir;
    56.    
    57.     //When to chase
    58.     public float chaseRadius = 20f;
    59.    
    60.     public float facePlayerFactor = 20f;
    61.    
    62.     void Awake()
    63.     {
    64.         nav = GetComponent<NavMeshAgent>();
    65.         //controller = GetComponentInParent<Animator>();
    66.         nav.enabled = true;
    67.     }
    68.    
    69.     //Use this for initialization
    70.     void Start()
    71.     {
    72.         waitTime = startWaitTime;
    73.         randomSpot = Random.Range(0, moveSpots.Length);
    74.         randomStrafeDir = Random.Range(0, 2);
    75.     }
    76.    
    77.     //Update is called once per frame
    78.     void Update()
    79.     {
    80.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    81.         if (distance <= losRadius)
    82.         {
    83.             CheckLOS();
    84.         }
    85.    
    86.         if (nav.isActiveAndEnabled)
    87.         {
    88.             if (playerIsInLOS == false && aiMemorizesPlayer == false && aiHeardPlayer == false)
    89.             {
    90.                 Patrol();
    91.                 NoiseCheck();
    92.            
    93.                 StopCoroutine(AiMemory());
    94.             }
    95.        
    96.             else if (aiHeardPlayer == true && playerIsInLOS == false && aiMemorizesPlayer == false)
    97.             {
    98.                 canSpin = true;
    99.                 GoToNoisePosition();
    100.             }
    101.        
    102.             else if (playerIsInLOS == true)
    103.             {
    104.                 aiMemorizesPlayer = true;
    105.                
    106.                 FacePlayer();
    107.            
    108.                 ChasePlayer();
    109.             }
    110.        
    111.             else if (aiMemorizesPlayer == true && playerIsInLOS == false)
    112.             {
    113.                 ChasePlayer();
    114.            
    115.                 StartCoroutine(AiMemory());
    116.             }
    117.         }
    118.         }
    119.    
    120.     //Hearing
    121.     void NoiseCheck()
    122.     {
    123.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    124.        
    125.         if (distance <= noiseTravelDistance)
    126.         {
    127.             if (SawSoundController.OnCollisionEnter()) <---- Cannot implicitly convert type `void' to `bool'
    128.             {
    129.                 noisePosition = PlayerMove.playerPos;
    130.                
    131.                 aiHeardPlayer = true;
    132.             }
    133.             else
    134.             {
    135.                 aiHeardPlayer = false;
    136.                
    137.                 canSpin = false;
    138.             }
    139.         }
    140.     }
    141.  
    142.     //Go to noise position
    143.     void GoToNoisePosition()
    144.     {
    145.         nav.SetDestination(noisePosition);
    146.        
    147.         if (Vector3.Distance(transform.position, noisePosition) <= 5f && canSpin == true)
    148.         {
    149.             isSpinningTime += Time.deltaTime;
    150.            
    151.             transform.Rotate(Vector3.up * spinSpeed, Space.World);
    152.            
    153.             if (isSpinningTime >= spinTime)
    154.             {
    155.                 canSpin = false;
    156.                 aiHeardPlayer = false;
    157.                 isSpinningTime = 0f;
    158.             }
    159.         }
    160.     }
    161.    
    162.     //Memory
    163.     IEnumerator AiMemory()
    164.     {
    165.         increasingMemoryTime = 0;
    166.        
    167.         while (increasingMemoryTime < memoryStartTime)
    168.         {
    169.             increasingMemoryTime += Time.deltaTime;
    170.             aiMemorizesPlayer = true;
    171.             yield return null;
    172.         }
    173.        
    174.         aiHeardPlayer = false;
    175.         aiMemorizesPlayer = false;
    176.     }
    177.    
    178.     //Line of sight
    179.     void CheckLOS()
    180.     {
    181.         Vector3 direction = PlayerMove.playerPos - transform.position;
    182.        
    183.         float angle = Vector3.Angle(direction, transform.forward);
    184.        
    185.         if (angle < fieldOfViewAngle * 0.5f)
    186.         {
    187.             RaycastHit hit;
    188.            
    189.             if (Physics.Raycast(transform.position, direction.normalized, out hit, losRadius))
    190.             {
    191.                 if (hit.collider.tag == "Player")
    192.                    
    193.                 {
    194.                     playerIsInLOS = true;
    195.                    
    196.                     aiMemorizesPlayer = true;
    197.                 }
    198.                 else
    199.                 {
    200.                     playerIsInLOS = false;
    201.                 }
    202.             }
    203.         }
    204.     }
    205.    
    206.     //Patrol
    207.     void Patrol()
    208.     {
    209.         nav.SetDestination(moveSpots[randomSpot].position);
    210.        
    211.         if (Vector3.Distance(transform.position, moveSpots[randomSpot].position)) <---- Cannot implicitly convert type `float' to `bool'
    212.         {
    213.             if (waitTime <=0)
    214.             {
    215.                 randomSpot = Random.Range(0, moveSpots.Length);
    216.                
    217.                 waitTime = startWaitTime;
    218.             }
    219.             else
    220.             {
    221.                 waitTime -= Time.deltaTime;
    222.             }
    223.         }
    224.     }
    225.    
    226.     //Chase player
    227.     void ChasePlayer()
    228.     {
    229.         float distance = Vector3.Distance(PlayerMove.playerPos, transform.position);
    230.        
    231.         if (distance <= chaseRadius && distance > distToPlayer)
    232.         {
    233.             nav.SetDestination(PlayerMove.playerPos);
    234.         }
    235.        
    236.         else if (nav.isActiveAndEnabled && distance <= distToPlayer)
    237.         {
    238.             randomStrafeDir = Random.Range(0, 2);
    239.             randomStrafeStartTime = Random.Range(t_minStrafe, t_maxStrafe);
    240.            
    241.             if (waitStrafeTime <= 0)
    242.             {
    243.                 if (randomStrafeDir == 0)
    244.                 {
    245.                     nav.SetDestination(strafeLeft.position);
    246.                 }
    247.                
    248.                 else if (randomStrafeDir == 1)
    249.                 {
    250.                     nav.SetDestination(strafeRight.position);
    251.                 }
    252.                 waitStrafeTime = randomStrafeStartTime;
    253.             }
    254.             else
    255.             {
    256.                 waitStrafeTime -= Time.deltaTime;
    257.             }
    258.         }
    259.     }
    260.    
    261.     //Face player
    262.     void FacePlayer()
    263.     {
    264.         Vector3 direction = (PlayerMove.playerPos - transform.position).normalized;
    265.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    266.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * facePlayerFactor);
    267.     }
    268. }
    The errors are:
    AI.cs(127,27): error CS0029: Cannot implicitly convert type `void' to `bool'
    AI.cs(211,15): error CS0029: Cannot implicitly convert type `float' to `bool'

    Please help....
     
  10. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
  11. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi sir! Even I know I am 12 and am left 68 years to live ( avg. 80 years lifespan for humans ), but even I have exams and can't focus all my time in learning C#, I need someone to help me or atleast teach me about syntax errors so, I atleast know the basics and what mistakes not to make, and even if I make how to correct them. I am not offended from your or SparrowsNest comment, but what I think is instead of telling me not to work on a small project and learn C# professionally maybe you can teach me the basics, so I learn C# on my own... I am not one of those code askers who just comment for code but I am here to ask for help for syntax errors I can't fix. Please don't be offended but read your comment once

    I am saying the same thing I am 12 and I want to learn but the basics and how to remove syntax errors first then those tough A.I's which can think on their own. The knowledge I have till date is because of learning from youtube and unity forums. Thank you!
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    A bool (short for Boolean variable) means a value that is either "true" or "false". Whenever you have an if statement, What you put in () must evaluate to either true or false.

    You're first error:
    You are expecting OnCollisionEnter to return true or false, depending if there is a collision, but that's not how this function works. If you want a function that immediately evaluates collisions, look at the Physics object's static methods
    https://docs.unity3d.com/ScriptReference/Physics.html

    Your second error:
    This doesn't make sense because Distance returns a number. A number is not true or false. You probably meant to put some comparison here. Something like "if distance < 3" or something like that.
     
  13. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536

    I'm not trying to offend, insult or hurt you in any way, on the contrary, I know EXACTLY where you coming from, first time i touched a software like this it was Flash when I was about your age (12-13), I too wanted to skip the learning curve and hacked/brute forced my way through it but it only made me more and more confused to the point of "F*** all this S***, i'm out" and i quit until i was 20 something and found out unity if free to use and jumped on it, by then I already had some experience with C#, 2D animation, sound creation and other related stuff, It still took a few good months until I learned Unity itself, honed my mostly theoretical C# knowledge into practical code, you are jumping ahead of your self and you're going to regret it later.
    you clearly lack basic knowledge on how all this stuff works so I'm strongly advising you to keep following tutorials, be it in school, books or youtube videos, they will teach you all you know want to know here.

    please understand that the knowledge you need right now is relatively very basic and there are endless sources you can get it for free, what we are here for mostly is help you debug you code, understand a specific concept and alike, not teach you 101.

    Whoa, little dude, going from basic syntax errors to full-on actual AI is quite a jump.
     
  14. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    To add a bit more help, check this guy out: https://www.youtube.com/playlist?list=PLbghT7MmckI7JAftgv0CUdU4RLNivG6wY

    This link is a basic C# tutorial by a guy who goes by the name "quill18", he's one of my favorite teachers(including all school teachers I've had and books too), he has a lot more on his channel "quill18creates" and I think it's a good starting point for you.

    If you want to get a little more advanced I'd suggest you look up sebastian lauge("SebastianLauge" on youtube), he's very good at explaining complicated stuff.(not saying quill is only "basic" stuff, he's got some great mid-high level stuff too)

    You've said you're interested in AI so this book should be worth a read after watching the first link I gave you(BTW I've red this book almost 3 times now and I've still got stuff to learn from it, don't give up just because you don't get it on the first go)
    http://lecturer.ukdw.ac.id/~mahas/dossier/gameng_AIFG.pdf

    Just trying to help you get on a path that leads you somewhere besides a brick wall, all the best wishes. ;)
     
  15. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Sorry Sir...
    in a simple word : Methodology
     
  16. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hey thank you everyone! I have finally fixed my code, it is working as expected.

    Ok sorry, I know was acting like a 18 month old, but that is because I was really stressed because of exams, homework, video games, and unity of course. Now I will try to act mature.

    Thank you! I will search quill18 and try to learn more.. thank you so much and best of wishes to you too for your projects.

    Thanks man, that really helped!

    Thank you sir, without you I couldn't fix my code...

    So overall, what I think is that I have to learn more, I have to try to act mature, I should not get irritated when someone is trying to help me, and finally I should always thank those who help me.;)
     
  17. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hey, how do i change my name? I want to change my name;)
     
  18. Deleted User

    Deleted User

    Guest

    Click on your account icon on the top right of the page, next to the search field, then select "Settings" and change your username. :)
     
  19. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    THANKS!
     
  20. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class PauseGame : MonoBehaviour
    7. {
    8.     public GameObject Pause;
    9.  
    10.     void Update () {
    11.     if(Input.GetButtonDown("Escape"))
    12.     {
    13.                  Time.timeScale = 0f;
    14.                  Pause.SetActive(true);
    15.     }
    16.     else
    17.     {
    18.                  Time.timeScale = 1f;
    19.                  Pause.SetActive(false);
    20.     }
    21. }
    22. }
    Any idea why this is not working? Have written pause menu many times but I don't know why when I am trying it with escape button it's not working...
     
  21. Deleted User

    Deleted User

    Guest

    Did you try with any other key instead of the "Escape"one?
     
  22. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Use Debug.Log to find out why! Hint, Google for "Unity Debug.Log example"
     
  23. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    if(Input.GetButtonDown("Escape")) not working because you have not Escape button in input system you need to change it with if(Input.GetKeyDown(KeyCode.Escape))
     
  24. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi! It's the updated code as suggested by @dontdiedevelop.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class PauseGame : MonoBehaviour
    5. {
    6.     public GameObject Pause;
    7.     void Update () {
    8.     if(Input.GetKeyDown(KeyCode.Escape))
    9.     {
    10.                  Pause.SetActive(true);
    11.                  Time.timeScale = 0f;
    12.     }
    13.     else
    14.     {
    15.                  Pause.SetActive(false);
    16.                  Time.timeScale = 1f;
    17.     }
    18. }
    19. }
    But it's still not working

    Yes, I did using almost all alphabetic keys.

    Haven't tried that out yet, but I am going too just now and will let you know the results.

    Umm.. it's not working..

    And, I have changed the code a bit..
     
  25. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Remember that GetKeyDown is only going to be true for one frame. On the very next frame it will be false, so therefore your code will fall to the "else" clause and it will deactivate your pause menu again.
     
  26. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Code (CSharp):
    1.  
    2. //Updated Code
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6. using UnityEngine.UI;
    7.  
    8. public class PauseGame: MonoBehaviour
    9. {
    10.  
    11.     public GameObject Pause;
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetKeyDown(KeyCode.Escape))
    16.         {
    17.             if (Time.timeScale == 1)
    18.             {
    19.                 Pause.SetActive(true);
    20.                 Time.timeScale = 0;
    21.             }
    22.             else if (Time.timeScale == 0)
    23.             {
    24.                 Pause.SetActive(false);
    25.                 Time.timeScale = 1;
    26.             }
    27.         }
    28.     }
    29. }
    It's working but only for else statement... in the editor when I make the "pause" gameobject active, and press escape.. it does disappear but only to not appear when I press escape again.

    Actually I didn't know that because it's my first time playing with keys.. earlier I used buttons.. So can you suggest any method??

    Actually that's what I think is happening.. I am tired so I guess I will work on it again tommorow..
     
  27. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    timeScale is a float, I believe, so its better to never compare using "==". Does it work if if you replace "== 0" and " ==1" with "<0.5f" and ">0.5f"?
     
  28. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PauseGame: MonoBehaviour
    6. {
    7.    
    8.     public GameObject Pause;
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetKeyDown(KeyCode.Escape))
    13.         {
    14.             if (Time.timeScale > 0.5f)
    15.             {
    16.                 Pause.SetActive(true);
    17.                 Time.timeScale = 0;
    18.             }
    19.             else if (Time.timeScale < 0.5f)
    20.             {
    21.                 Pause.SetActive(false);
    22.                 Time.timeScale = 1;
    23.             }
    24.         }
    25.     }
    26. }
    No, it didn't work.. I guess I have to edit my code again..
     
  29. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    I just made the code a lot simpler by seperating the pause and resume methods and adding a static bool.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PauseGame: MonoBehaviour
    6. {
    7.  
    8.     public GameObject PauseMenu;
    9.     public static bool GameIsPaused = false;
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.Escape))
    14.         {
    15.             if (GameIsPaused)
    16.             {
    17.                 Resume();
    18.             }
    19.             else
    20.             {
    21.                 Pause();
    22.             }
    23.         }
    24.     }
    25.  
    26.     void Resume()
    27.     {
    28.                 PauseMenu.SetActive(false);
    29.                 Time.timeScale = 1f;
    30.                 GameIsPaused = false;
    31.     }
    32.  
    33.     void Pause()
    34.     {
    35.                 PauseMenu.SetActive(true);
    36.                 Time.timeScale = 0f;
    37.                 GameIsPaused = true;
    38.     }
    39. }
    But, the same thing is happening again and again...
     
    Last edited: Apr 4, 2019
  30. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Just to make sure, the "Pause" gameobject that you are setting inactive- Is that a different object than your PauseGame script is attached to?
     
  31. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How are you debugging? I don't see any Debug.Log statements. Perhaps step debugging in Visual Studio?
     
  32. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi! but I can't figure out how to use Debug.log... I am still in the learning process..
     
  33. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Yes! It's attached to the canvas which contains the panel gameobject "PauseMenu".
     
  34. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hey, guys I am sorry but because of issues I will make it a mobile game, so yeah I will use a button and it's working as expected with a button. So yeah, sorry for disturbing you guys, but I am happy that you helped. Just one last help... I am finished with the idea of my new enemy AI ( this AI is much different than the complex AI this thread was based on ). I want my enemy to run towards my player when the player sees it but more like a slender based ai.. as it would randomly ( not really ) teleport in the terrain... Now.. i need help only in the "I want my enemy to run towards my player when the player sees it" and "it would randomly ( not really ) teleport in the terrain" parts.. any help would be appreciated.. I have not started making the AI yet but I am confident that this AI wouldn't be that complex.. I will upload my code work in some days if there are any issues.. Thank You!
     
  35. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    I assure you, if you don't know basics tools for debugging, including Debug.Log and break points, going further in game dev, soon become nightmare.

    So I suggest, learn at least basics methods of debugging, as soon as possible. You will appreciate spent own time then.
     
    SparrowGS and tcmeric like this.
  36. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    I guess, you're right I think I should give some time for learning... thanks!
     
  37. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Is there any way to delete this thread? I have got my answers and all...
     
  38. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    Do not delete your thread, newbies are everywhere :p sometime someone can need this posts
     
    Antypodish likes this.
  39. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Indeed, please never delete your posts (like first post was deleted already), once in discussion.
    It has contribution to thread and community.
    Otherwise, this can be viewed, as unfriendly act against posters and readers.
     
    SparrowGS likes this.
  40. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Ok.. I will not delete this thread, I promise. But can you tell how to delete? I will not delete, don't worry.
     
  41. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    I am not worry, because you can't just like that. You can delete, only soon after new thread is created. And I am not even 100% sure you can then, by yourself. But once topic is receiving replays, you can't for sure. The only other way, is to request deletion from moderators. And that would need to be with rational explanation, for such reason.
     
    Last edited: Apr 5, 2019
    JeffDUnity3D likes this.
  42. Deleted User

    Deleted User

    Guest

    No, you can't; I tried with a thread I created that never got any comments; there is no way to delete the first post, deleting the entire thread as well. :)
     
    Antypodish likes this.
  43. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Thanks to everyone who replied.. I will now not be active here.. but I will return someday and tell you all that I improved a lot and maybe even give a link to my game.. Bye guys..
     
  44. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Dont keep us waiting too long now ;)
     
    Antypodish likes this.
  45. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi guys... just wanted to give updates..

    I learnt how to use Debug.Log... in fact I am using it in a small 3D game i am making just to enhance my coding, it'so useful, it's so shameful I didn't knew about this earlier.. It's all about avoiding balls which will randomly come near you.. It's just a test project, So.. I will not release it..

    I learnt almost all about Vector3.. in fact obviously I am using it!!

    Plus, I was really shocked to see so many slender script demands on unity forums... I mean it's so easy but yes it's complex too... I have created one and it works as expected..

    But.. I am not finished I still have to learn more.. but I will release my first ( not really ) game in june or july.. was practising After Effects side by side.. Bye!

    Code (CSharp):
    1. Debug.Log("look duration = " + lookDuration);
    2.  
    3. // This is the part of my code which uses Debug.Log.. it's really useful
     
    SparrowGS and Antypodish like this.
  46. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    I corrected my whole code and this time it worked!! Thanks to @JeffDUnity3D who introduced Debug.Log to me..
     
  47. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Update #2
    I finished my test project, It was a success. Now, I am moving to a little bit bigger. I am making a SLENDER game, don't get me wrong, I making it as my test project but I will share the game to the public for rating. Before you say that the scripts are hard, I want to tell you I already have them, I created them. They work perfectly! Plus all scripts you will see in that project will be written by me. I hope you support me. I already created all scripts, now I just have to create the terrain and all. I will link that game's trailer in two weeks and then the game itself three weeks later.
     
  48. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Update #3
    I have changed my plans a bit. The slender game I am creating will be converted to an asset. I want to help people create what they want too.. It's going to be simple but really good, I promise :)

    It will include:
    1) Menu Scene
    1.1) Fully functional simple main menu with video background, play, quit, credits, youtube and discord buttons.

    2) Loading Scene
    2.1) Random text displayer just like those hint box. Will automatically turn to Game scene after some time

    3) Game Scene (The real deal)
    3.1) Slender AI (Spawns more often depending on your page count)
    3.2) Page Pickup and Random Spawner System
    3.3) Fully functional simple pause menu with Sensitivity Controller and Music Controller. You can add your own things.
    3.5) Glitch/Static Effect

    4) DeathCam Scene
    4.1) Will automatically open if player stares slender for too long
    4.2) SFX
    4.3) Glitch/Static Effect

    5) GameOver Scene (Really Simple)
    5.1) Will automatically open after some time
    5.2) GameOver text and button to go back to menu

    0 Compiler Errors and Well commented scripts. I mean this asset may be simple but I guess useful for many people who want to create slender games.

    Well really if you ask me.. I never want to create horror games that's why I am turning this into an asset.

    I would like your opinion on this subject though..
     
  49. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
  50. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Update #4
    Hi! I want to share my first test project (2d) with everyone (not the source code). I was inspired by Therse's asset "Bouncing Rabbit", but because my parents didn't allow me to buy that asset, I took the challenge to recreate it. Almost all sprites in my game has been taken from the video trailer, though I couldn't get that fire trail image, so I created one using photoshop. and I recorded the music and used Audacity to remove the noise. It all took me 3 months, and I am pretty impressed with my project from the menu to the game over scene. Don't get me wrong it was just a test project and I have no intentions to sell my project (all credits go to the actual creator). You can check out my project and trailer here.