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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

RaycastHit not working on terrain

Discussion in 'Scripting' started by Sean-Powell, Jul 29, 2015.

  1. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    i am using a raycast to move a nave mash agent on a terrain but it will not register the hit on the terrain but it will register hits of objects on the terrain, the terrain has a box collider around it, and it also give the collider.tag as terrain but always sets the position to the Bottom left corner of the terrain no matter where i click any ideas?

    this is the method :
    Code (CSharp):
    1.     void castRay()
    2.     {
    3.         Debug.Log("draw");
    4.         Ray ray = camera.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
    5.         RaycastHit hit = new RaycastHit();
    6.         Debug.DrawRay(ray.origin, ray.direction * 10, Color.red);
    7.         if (Physics.Raycast(ray, out hit, 10000))
    8.         {
    9.             Debug.Log(hit.collider.name);
    10.             float x = hit.transform.position.x;
    11.             float z = hit.transform.position.z;
    12.             Vector3 hitPosition = new Vector3(x, 0.61f, z);
    13.             Debug.Log(hitPosition);
    14.             selectedUnit.GetComponent<NavMeshAgent>().SetDestination(hitPosition);
    15.         }
    16.     }
    17. }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    What you're looking for is "hit.point" not "hit.transform". The former refers to the impact point from the ray with the object, whereas the latter refers to the transform of the object that was hit. The transform has no information about the raycast- it's just a component of the terrain.
     
    GroZZleR and eisenpony like this.
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I think you want hit.point instead of hit.transform.position

    Code (CSharp):
    1.     void castRay()
    2.     {
    3.         Debug.Log("draw");
    4.         Ray ray = camera.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
    5.         RaycastHit hit = new RaycastHit();
    6.         Debug.DrawRay(ray.origin, ray.direction * 10, Color.red);
    7.         if (Physics.Raycast(ray, out hit, 10000))
    8.         {
    9.             Debug.Log(hit.collider.name);
    10.             float x = hit.point.x;
    11.             float z = hit.point.z;
    12.             Vector3 hitPosition = new Vector3(x, 0.61f, z);
    13.             Debug.Log(hitPosition);
    14.             selectedUnit.GetComponent<NavMeshAgent>().SetDestination(hitPosition);
    15.         }
    16.     }
    17. }
     
    GroZZleR likes this.
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You want to use hit.point.x and hit.point.z, not the transform of the object that was hit (which will always be a static position like 0,0 as you're finding out).

    EDIT: LOL :D
     
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Too many of us today! :confused:
     
    TEBZ_22 likes this.
  6. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    yea thanks guys