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 Terrain Collision Detection

Discussion in 'Scripting' started by Falcoshin, Jan 23, 2021.

  1. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    I'm trying to have a character controller move from one point to another on a mostly flat terrain with the exception of a small bump in the terrain between its starting point and its destination. I'm also trying to get the character controller to detect the small bump as and when it moves with the following code, but for whatever reason it never sees the bump. Why is that?

    Code (csharp):
    1.  
    2. //Move towards destination
    3.         if (transform.position != destination)
    4.         {
    5.             //Set angle and magnitude of movement
    6.             float angle = AngleBetween(transform.position, destination);
    7.             float magnitude = Time.deltaTime * SPEED;
    8.             transform.eulerAngles = Vector3.up * angle;
    9.             if (magnitude > Vector3.Distance(transform.position, destination))
    10.             {
    11.                 magnitude = Vector3.Distance(transform.position, destination);
    12.             }
    13.  
    14.             //Obstacle checking
    15.             float radius = cc.radius * transform.localScale.x + cc.skinWidth;
    16.             RaycastHit[] hits = Physics.CapsuleCastAll(CapsuleBottom(transform.position, radius), CapsuleTop(transform.position, radius), radius, AngleToVector(angle), magnitude);
    17.             bool moveCheck = true;
    18.  
    19.             if (hits.Length > 1)
    20.             {
    21.                 print(true);
    22.             }
    23.  
    24.             //Movement block
    25.             if (!moveCheck)
    26.             {
    27.                 print("Insurmountable terrain detected");
    28.             }
    29.             else
    30.             {
    31.                 if (magnitude == Vector3.Distance(transform.position, destination))
    32.                 {
    33.                     transform.position = destination;
    34.                 }
    35.                 else
    36.                 {
    37.                     cc.Move(AngleToVector(angle) * magnitude);
    38.                 }
    39.             }
    40.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
    Do you ever set moveCheck to false above? I don't see it. I would expect it somewhere around line 21 inside that if clause.
     
  3. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Not necessary at this point. What matters is that the if clause is triggered AT ALL and it's not. That's the problem.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
    Is CapsuleBottom immediately against the terrain? If so, a non-zero-sized capsule will already be IN the terrain when you cast so it won't hit the terrain.

    Generally speaking you gotta be outside of and cast into a collider to have a chance of it hitting.
     
  5. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Which is why the capsule cast being tested is in the direction the character is moving and as far as the character should move that frame. The thought process was suppose to be that, as long as the vector of the capsule cast has no y component (ensured by the AngleToVector method) that it should snag itself on terrain if it's above where the character is at present.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
    I guess that depends on what is inside CapsuleBottom, CapsuleTop and AngleToVector!

    Try hard numbers: make some invisible gameobjects you can move around in the scene, use them to make a capsule cast that you know is 100% above all the terrain, casting forward at the bump, see if it hits.

    Otherwise we can do this all day long. You could be hitting yourself for all we know, or starting inside yourself so it ignores everything else. Isolate, isolate, isolate.
     
  7. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    That's exactly what this code is doing and it's not finding the terrain.