Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New with Csharp need à little help

Discussion in 'Scripting' started by warrox, May 10, 2021.

  1. warrox

    warrox

    Joined:
    Dec 20, 2020
    Posts:
    1
    Hey,
    i'm creating a script with a void on Trigger enter and on Collision enter.
    The Trigger part is working well but i don't understand why the Collision enter part is not detecting when my both Object are touching eachother.
    I specifiy both objects have a box collider and one of them have a rigidbody
    my goal is to active the trigger on the animator and play the animation when both are in collision
    thanks guys.
    here is my code :

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Drag : MonoBehaviour
    7. {
    8.  
    9.     public float speed = 2f;
    10.     public float run = 4f;
    11.     public float rotationSpeed = 4f;
    12.  
    13.     public bool ColidContact = false;
    14.     public GameObject targetOfDrag;
    15.     Rigidbody rb;
    16.  
    17.     [SerializeField]
    18.     GameObject Ennemy;
    19.    
    20.  
    21.      void Start()
    22.     {
    23.  
    24.         Ennemy.GetComponent<Animator>();
    25.  
    26.         targetOfDrag.GetComponent<Transform>();
    27.         rb = GetComponent<Rigidbody>();
    28.     }
    29.  
    30.  
    31.     IEnumerator WaitForWalk()
    32.     {
    33.         if (ColidContact == true)
    34.         {
    35.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetOfDrag.transform.position - transform.position), rotationSpeed * Time.deltaTime);
    36.             Ennemy.GetComponent<Animator>().SetBool("TriggerOn", true);
    37.             yield return new WaitForSeconds(0.6f);
    38.             transform.position += transform.forward * Time.deltaTime * speed;
    39.             // Ennemy.transform.Translate(Vector3.forward * speed * Time.deltaTime);
    40.  
    41.  
    42.  
    43.         }
    44.     }
    45.     void Update()
    46.     {
    47.  
    48.  
    49.  
    50.         StartCoroutine("WaitForWalk");
    51.        
    52.      
    53.        
    54.  
    55.     }
    56.  
    57.     private void OnTriggerEnter(Collider other)
    58.     {
    59.  
    60.        
    61.        
    62.       ColidContact = true;
    63.  
    64.     }
    65.  
    66.     // try to put activ a boolean in this method and read the code
    67.     // in the Coroutine
    68.     private void OnCollisionEnter(Collision collision)
    69.     {
    70.         if (collision.collider.CompareTag("Player"))
    71.            
    72.         {
    73.           // rb.isKinematic = false;
    74.             //rb.detectCollisions = true;
    75.             Ennemy.GetComponent<Animator>().SetTrigger("CollisionDetected");
    76.  
    77.         }
    78.  
    79.     }
    80.  
    81.  
    82.  
    83.  
    84. }
    85.  
    86.  
    [ICODE][ICODE]
    [/ICODE][/ICODE]
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    A collider cannot be both a trigger and a non-trigger. OnTriggerXXX is called for triggers and OnCollisionXXX for non-triggers.
     
    Joe-Censored and warrox like this.