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. Dismiss Notice

Question Is there a problem at OverlapCircleAll? Contact Diameter (temasCapi) couldn't be associated..

Discussion in 'Scripting' started by TheS520ex, Aug 31, 2023.

  1. TheS520ex

    TheS520ex

    Joined:
    Aug 16, 2023
    Posts:
    8
    Hello everyone,

    Moving, attacking,sliding and other animations are worked. I tried to add the jumping (zipla) code before the jumping animations( I haven't come to that part yet), but it failed...
    My codes worked up to here. :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     private Rigidbody2D myRigidbody;
    10.     private Animator myAnimator;
    11.  
    12.     [SerializeField]
    13.     private GameObject anahtarVar;
    14.  
    15.     [SerializeField]
    16.     private float hiz;
    17.    
    18.     private bool sagaBak;
    19.  
    20.     [SerializeField]
    21.     private AudioSource coinSes;
    22.  
    23.     [SerializeField]
    24.     private AudioSource anahtarSes;
    25.  
    26.     [SerializeField]
    27.     private Transform[] temasNoktasi;
    28.     [SerializeField]
    29.     private float temasCapi;
    30.     [SerializeField]
    31.     private LayerMask hangiZemin;
    32.  
    33.     private bool zeminde;
    34.     private bool zipla;
    35.     [SerializeField]
    36.     private bool havadaKontrol;
    37.     [SerializeField]
    38.     private float ziplamaKuvveti;
    39.  
    40.     private bool atak;
    41.     private bool kayma;
    42.  
    43.     // Start is called before the first frame update
    44.     void Start()
    45.     {
    46.         sagaBak = true;
    47.         myRigidbody = GetComponent<Rigidbody2D>();
    48.         myAnimator = GetComponent<Animator>();
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.         UnityEngine.Debug.Log("Controls are Working");
    55.         Kontroller();
    56.     }
    57.  
    58.     void FixedUpdate()
    59.     {
    60.         float yatay = Input.GetAxis("Horizontal");
    61.         TemelHareketler(yatay);
    62.         YonCevir(yatay);
    63.         AtakHareketleri();
    64.         DegerleriResetle();
    65.     }
    66.  
    67.     private void TemelHareketler(float yatay)
    68.     {
    69.         Debug.Log("TemelHareketler fonksiyonu �al���yor.");
    70.         if (!myAnimator.GetBool("kayma") && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsTag("atak"))
    71.         {
    72.             Debug.Log("Start moving");
    73.             myRigidbody.velocity = new Vector2  (yatay*hiz,myRigidbody.velocity.y);
    74.         }
    75.  
    76.         if (kayma && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName ("Slide"))
    77.         {
    78.             Debug.Log("Start sliding");
    79.             myAnimator.SetBool("kayma", true);
    80.         }
    81.         else if (!this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName("Slide"))
    82.         {
    83.             Debug.Log("Stop sliding");
    84.             myAnimator.SetBool("kayma", false);
    85.         }
    86.         myAnimator.SetFloat("karakterHizi", Mathf.Abs(yatay));
    87.     }
    88.    
    89.     private void AtakHareketleri()
    90.     {
    91.         UnityEngine.Debug.Log("Trigger and vector2 zero are Working");
    92.         if (atak && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("atak"))
    93.             {
    94.             myAnimator.SetTrigger("atak");
    95.             myRigidbody.velocity = Vector2.zero;
    96.             }
    97.     }
    98.  
    99.     private void Kontroller()
    100.     {
    101.         if (Input.GetKeyDown(KeyCode.T))
    102.         {
    103.             myAnimator.SetTrigger("atak");
    104.         }
    105.  
    106.         if (Input.GetKeyDown(KeyCode.Y))
    107.         {
    108.             myAnimator.SetTrigger("kayma");
    109.         }
    110.     }
    111.     private void YonCevir(float yatay)
    112.     {
    113.         if (yatay > 0 && !sagaBak || yatay < 0 && sagaBak)
    114.         {
    115.             sagaBak = !sagaBak;
    116.             Vector3 yon = transform.localScale;
    117.             yon.x *= -1;
    118.             transform.localScale = yon;
    119.         }
    120.     }
    121.     void OnCollisionEnter2D(Collision2D other)
    122.     {
    123.         UnityEngine.Debug.Log("Coin collecting and is Working");
    124.         if (other.gameObject.tag == ("altin"))
    125.         {
    126.             other.gameObject.SetActive(false);
    127.             coinSes.Play();
    128.         }
    129.         else if (other.gameObject.tag == ("gumus"))
    130.         {
    131.             other.gameObject.SetActive(false);
    132.             coinSes.Play();
    133.         }
    134.         else if (other.gameObject.tag == ("bronz"))
    135.         {
    136.             other.gameObject.SetActive(false);
    137.             coinSes.Play();
    138.         }
    139.         if (other.gameObject.tag == ("anahtar"))
    140.         {
    141.             other.gameObject.SetActive(false);
    142.             anahtarVar.SetActive(true);
    143.             anahtarSes.Play();
    144.         }
    145.     }
    146.     private void DegerleriResetle()
    147.     {
    148.         UnityEngine.Debug.Log("Reseting the values are Working");
    149.         atak = false;
    150.         kayma = false;
    151.     }
    152. }
    153.  
    154.  
    After I added this part, it didn't work:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     private Rigidbody2D myRigidbody;
    10.     private Animator myAnimator;
    11.  
    12.     [SerializeField]
    13.     private GameObject anahtarVar;
    14.  
    15.     [SerializeField]
    16.     private float hiz;
    17.    
    18.     private bool sagaBak;
    19.  
    20.     [SerializeField]
    21.     private AudioSource coinSes;
    22.  
    23.     [SerializeField]
    24.     private AudioSource anahtarSes;
    25.  
    26.     [SerializeField]
    27.     private Transform[] temasNoktasi;
    28.     [SerializeField]
    29.     private float temasCapi;
    30.     [SerializeField]
    31.     private LayerMask hangiZemin;
    32.  
    33.     private bool zeminde;
    34.     private bool zipla;
    35.     [SerializeField]
    36.     private bool havadaKontrol;
    37.     [SerializeField]
    38.     private float ziplamaKuvveti;
    39.  
    40.     private bool atak;
    41.     private bool kayma;
    42.  
    43.     // Start is called before the first frame update
    44.     void Start()
    45.     {
    46.         sagaBak = true;
    47.         myRigidbody = GetComponent<Rigidbody2D>();
    48.         myAnimator = GetComponent<Animator>();
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.         UnityEngine.Debug.Log("Controls are Working");
    55.         Kontroller();
    56.     }
    57.  
    58.     void FixedUpdate()
    59.     {
    60.         float yatay = Input.GetAxis("Horizontal");
    61.         TemelHareketler(yatay);
    62.         zeminde = Zeminde();  
    63.         YonCevir(yatay);
    64.         AtakHareketleri();
    65.         DegerleriResetle();
    66.     }
    67.  
    68.     private void TemelHareketler(float yatay)
    69.     {
    70.         Debug.Log("TemelHareketler fonksiyonu �al���yor.");
    71.         if (!myAnimator.GetBool("kayma") && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsTag("atak") && (zeminde||havadaKontrol))
    72.         {
    73.             Debug.Log("Start moving");
    74.             myRigidbody.velocity = new Vector2  (yatay*hiz,myRigidbody.velocity.y);
    75.         }
    76.  
    77.         if (zeminde && zipla)
    78.         {
    79.             zeminde = false;
    80.             myRigidbody.AddForce(new Vector2(0, ziplamaKuvveti));
    81.         }
    82.  
    83.         if (kayma && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName ("Slide"))
    84.         {
    85.             Debug.Log("Start sliding");
    86.             myAnimator.SetBool("kayma", true);
    87.         }
    88.         else if (!this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName("Slide"))
    89.         {
    90.             Debug.Log("Stop sliding");
    91.             myAnimator.SetBool("kayma", false);
    92.         }
    93.         myAnimator.SetFloat("karakterHizi", Mathf.Abs(yatay));
    94.     }
    95.    
    96.     private void AtakHareketleri()
    97.     {
    98.         UnityEngine.Debug.Log("Trigger and vector2 zero are Working");
    99.         if (atak && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("atak"))
    100.             {
    101.             myAnimator.SetTrigger("atak");
    102.             myRigidbody.velocity = Vector2.zero;
    103.             }
    104.     }
    105.  
    106.     private void Kontroller()
    107.     {
    108.         if (Input.GetKeyDown (KeyCode.U))
    109.         {
    110.             zipla = true;
    111.         }
    112.  
    113.         if (Input.GetKeyDown(KeyCode.T))
    114.         {
    115.             myAnimator.SetTrigger("atak");
    116.         }
    117.  
    118.         if (Input.GetKeyDown(KeyCode.Y))
    119.         {
    120.             myAnimator.SetTrigger("kayma");
    121.         }
    122.     }
    123.     private void YonCevir(float yatay)
    124.     {
    125.         if (yatay > 0 && !sagaBak || yatay < 0 && sagaBak)
    126.         {
    127.             sagaBak = !sagaBak;
    128.             Vector3 yon = transform.localScale;
    129.             yon.x *= -1;
    130.             transform.localScale = yon;
    131.         }
    132.     }
    133.     void OnCollisionEnter2D(Collision2D other)
    134.     {
    135.         UnityEngine.Debug.Log("Coin collecting and is Working");
    136.         if (other.gameObject.tag == ("altin"))
    137.         {
    138.             other.gameObject.SetActive(false);
    139.             coinSes.Play();
    140.         }
    141.         else if (other.gameObject.tag == ("gumus"))
    142.         {
    143.             other.gameObject.SetActive(false);
    144.             coinSes.Play();
    145.         }
    146.         else if (other.gameObject.tag == ("bronz"))
    147.         {
    148.             other.gameObject.SetActive(false);
    149.             coinSes.Play();
    150.         }
    151.         if (other.gameObject.tag == ("anahtar"))
    152.         {
    153.             other.gameObject.SetActive(false);
    154.             anahtarVar.SetActive(true);
    155.             anahtarSes.Play();
    156.         }
    157.     }
    158.     private void DegerleriResetle()
    159.     {
    160.         UnityEngine.Debug.Log("Reseting the values are Working");
    161.         atak = false;
    162.         kayma = false;
    163.         zipla = false;
    164.     }
    165.     private bool Zeminde()
    166.     {
    167.         if (myRigidbody.velocity.y <= 0)
    168.         {
    169.             foreach (Transform nokta in temasNoktasi)
    170.             {
    171.                 Collider2D[] colliders = Physics2D.OverlapCircleAll(nokta.position, temasCapi, hangiZemin);
    172.  
    173.                 for (int = 0; i < colliders.Length; i++)
    174.                 {
    175.                     if (colliders[i].gameObject != gameObject)
    176.                     {
    177.                         return true;
    178.                     }
    179.                 }
    180.             }
    181.         }
    182.         return false;
    183.     }
    184. }
    185.  
    186.  
    Probably the problem is in the last part.(Zeminde (on the ground)). Can anyone help me to fix this?
    Thanks a lot.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You'll need to elaborate on what "it didn't work" actually constitutes.
     
    Bunny83 likes this.
  3. TheS520ex

    TheS520ex

    Joined:
    Aug 16, 2023
    Posts:
    8
    It writes on the screen all compilers errors have to be fixed before you can enter playmode...
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Uhm, do you think we are human compilers and will now parse your code to find "some" issue with it? The compiler already told you what he has a problem with. You got an error message as well as a file, line and column number where the error occured in the file. You got that information, we don't. So check the console in Unity, select the error and you can copy the error message from the bottom half of the console window.
     
  5. TheS520ex

    TheS520ex

    Joined:
    Aug 16, 2023
    Posts:
    8
    Ok guys, thanks a lot anyway, chatgpt solved my mistake. I made mistake in this line:
    for (int = 0; i < colliders.Length; i++)
    int i= 0; lol