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

CharacterController Custom "Collision Detection"

Discussion in 'Scripting' started by ChadrickEvans, Apr 21, 2015.

  1. ChadrickEvans

    ChadrickEvans

    Joined:
    Mar 14, 2015
    Posts:
    46
    Hi everybody,

    I just started using Unity about a month ago and one thing that really bugged me about the CharacterController was it's inability to detect collisions while standing still. I've always wanted to make a Third Person video game where you can perform melee attacks (punching, kicking, grabbing) but ControllerColliderHit is just SO limited.

    I have attempted alternative colliders, switching over to rigidbodies and even "tricking" Unity into thinking the enemy was touching my player character but I could never get it to work quite right. Luckily, the extremely helpful documentation guide helped me merge a few lines of code into a successful script.

    Here it is for anybody who's trying to accomplish the same thing.
    Code (CSharp):
    1. public class EnemyTargets : MonoBehaviour {
    2.     //reference the public/private variables you will be using.
    3.     //the public string is only used to test which enemy is closest to your player.
    4.     GameObject enemy;
    5.     GameObject player;
    6.     public string collideObj;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         //assign the player GameObject reference to the name of your main character (or tag, whichever suits you).
    11.         player = GameObject.Find("InsertPlayerName");
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         //run a foreach statement to access each GameObject with a tag of "Enemy".
    17.         foreach(GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy")){
    18.             //If the distance between one of these enemys is lower than 2.0f than the string will target that enemy.
    19.             //Keep in mind this chooses the absolute closest. If there is a crowd only one enemy will be chosen.
    20.             if(Vector3.Distance(player.transform.position, enemy.transform.position) <= 2f){
    21.                 collideObj = enemy.name;
    22.             }
    23.             //use an else if statement to clear the previously targetted enemy if it is no longer the closest one.
    24.             else if(Vector3.Distance(player.transform.position, enemy.transform.position) > 2f && collideObj == enemy.name){
    25.                 collideObj = "";
    26.             }
    27.         }
    28.     }
    29. }



    (Also, I know using Vector3.distance is expensive. I've heard about using sqrMagnitude instead but I haven't figured out how to implement it just yet.)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148


    Vector3.Distance is "expensive" because it has to computer a squareroot. If you use sqrMagnitude instead you "just" need to compare it to the square'd value (i.e. 2*2 = 4).