Search Unity

DrawLine vs DrawRay = different results...

Discussion in 'Scripting' started by WheresMommy, May 19, 2015.

  1. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Hi there everybody. As I am working on my game, I had the idea of a scanning system, which shoots rays in a spherical way and everywhere where it hits something, it instantiates a glowbulb or sth.
    I started with the Debug.DrawRay method and figured out, with a not very nice code how to raycast it the way I want.


    Then I wanted to instantiate the prefabs where the ray possibly hits the surrounding, but it did not work, as my prefabs seemed to be instantiated in the world coordinates, maybe. then I tried the DrawLine Method to check if I am doing something wrong and this came out:

    So, I do not know, where the difference is, but maybe you guys can tell me what I am doing wrong, as it is late here and I do not want to mess up everything while breaking with my head through the wall ;)

    Thanks in advance!! here is the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class RaycastMenu : MonoBehaviour {
    6.     public GameObject RayHit ;
    7.  
    8.     private Vector3 direction;
    9.     private List<Vector3> RayCasts = new List<Vector3>() ;
    10.     private int RayCount = 0;
    11.     private int RayQuarterCount = 6 ;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         direction = new Vector3 (1, 0, 0);
    16.         CreateArray ();
    17.     }
    18.    
    19.     // Draw all rays
    20.     void Update () {
    21.         foreach (Vector3 rayItem in RayCasts) {
    22.             Debug.DrawRay(transform.position, rayItem);
    23.             //Debug.DrawLine(transform.position, rayItem);
    24.         }
    25.     }
    26.  
    27.     void CreateArray(){
    28.         float currentRay = RayCount ;
    29.  
    30.         for (RayCount = RayCount; RayCount <= RayQuarterCount * 4 * 4 * 2; RayCount++)
    31.         {
    32.             RayCasts.Add(new Vector3(direction.x, direction.y, direction.z));
    33.  
    34.             currentRay = RayCount - Mathf.Round(RayCount/24) * 24 ;
    35.             direction.y = -0.5f + Mathf.Round(RayCount/24) * 0.15f;
    36.  
    37.             if (currentRay < RayQuarterCount){
    38.                 direction.x = direction.x - 0.15f ;
    39.                 direction.z = direction.z + 0.15f ;
    40.             }else if (currentRay < RayQuarterCount * 2){
    41.                 direction.x = direction.x - 0.15f ;
    42.                 direction.z = direction.z - 0.15f ;
    43.             }else if (currentRay < RayQuarterCount * 3){
    44.                 direction.x = direction.x + 0.15f ;
    45.                 direction.z = direction.z - 0.15f ;
    46.             }else if (currentRay < RayQuarterCount * 4){
    47.                 direction.x = direction.x + 0.15f ;
    48.                 direction.z = direction.z + 0.15f ;
    49.             }else{
    50.                 direction.x = 1 ;
    51.                 direction.z = 0 ;
    52.             }
    53.  
    54.             Debug.Log (currentRay);
    55.         }
    56.     }
    57. }
    58.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    DrawLine draws a line between two points, DrawRay draw a ray with a position and a direction.

    --Eric
     
    vDimitris, Filip8429 and sean244 like this.
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Thanks for the clarification about the Debug commands! :) I think, I made my problem not clear enough, as I wrote it down the wrong way :D My problem is, that, if I raycast, I need the exact position on the surface. Currently, if I am using
    Code (CSharp):
    1. for (int i = 0; i <= RayQuarterCount; i++){
    2.             RaycastHit hit ;
    3.             if (Physics.Raycast(transform.position, RayCasts[i], out hit, 1000.0F)){
    4.                 Debug.Log (hit.transform.position);
    5.                 Instantiate(RayHit, hit.transform.position, Quaternion.identity);
    6.             }
    7.             //Debug.DrawLine(transform.position, rayItem);
    8.         }
    my instances are at the position of the pivot point on every object, but I need the exact position of the ray hit position. Maybe someone can help me or push me in the right direction :)
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Hit.point? I think hit.transform.position will give you the transform position of whatever object the ray hit
     
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Yep, it gives me the position of the object, but not the point where the ray hits the object :) the hit.transform.position just returns the pivot transform of the object. So think of a wall with the pivot in the middle. I shoot like 10 rays on it and want to have the exact point on the wall. For now, it will instantiate only in the middle because this is the transform.position of the wall :)
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    I meant this: http://docs.unity3d.com/ScriptReference/RaycastHit-point.html
     
    LeftyRighty likes this.
  7. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You sir, are the boss ;) Thanks for that!


    I just change my current code to hit.point you were suggesting, and it works like a charm! :)
    So for everyone who is interested, it looks like this now.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class RaycastMenu : MonoBehaviour {
    6.     public GameObject RayHit ;
    7.  
    8.     private Vector3 direction;
    9.     private List<Vector3> RayCasts = new List<Vector3>() ;
    10.     private int RayCount = 0;
    11.     private int RayQuarterCount = 6 ;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         direction = new Vector3 (1, 0, 0);
    16.         CreateArray ();
    17.     }
    18.    
    19.     // Draw all rays
    20.     void DrawPoints () {
    21.         for (int i = 0; i <= RayQuarterCount * 4 * 4 * 2; i++){
    22.             RaycastHit hit ;
    23.             if (Physics.Raycast(transform.position, RayCasts[i], out hit, 1000.0F)){
    24.                 Debug.Log (hit.transform.position);
    25.                 Instantiate(RayHit, hit.point, Quaternion.identity);
    26.             }
    27.             //Debug.DrawLine(transform.position, rayItem);
    28.         }
    29.     }
    30.  
    31.     void CreateArray(){
    32.         float currentRay = RayCount ;
    33.  
    34.         for (RayCount = RayCount; RayCount <= RayQuarterCount * 4 * 4 * 2; RayCount++)
    35.         {
    36.             RayCasts.Add(new Vector3(direction.x, direction.y, direction.z));
    37.  
    38.             currentRay = RayCount - Mathf.Round(RayCount/24) * 24 ;
    39.             direction.y = -0.5f + Mathf.Round(RayCount/24) * 0.15f;
    40.  
    41.             if (currentRay < RayQuarterCount){
    42.                 direction.x = direction.x - 0.15f ;
    43.                 direction.z = direction.z + 0.15f ;
    44.             }else if (currentRay < RayQuarterCount * 2){
    45.                 direction.x = direction.x - 0.15f ;
    46.                 direction.z = direction.z - 0.15f ;
    47.             }else if (currentRay < RayQuarterCount * 3){
    48.                 direction.x = direction.x + 0.15f ;
    49.                 direction.z = direction.z - 0.15f ;
    50.             }else if (currentRay < RayQuarterCount * 4){
    51.                 direction.x = direction.x + 0.15f ;
    52.                 direction.z = direction.z + 0.15f ;
    53.             }else{
    54.                 direction.x = 1 ;
    55.                 direction.z = 0 ;
    56.             }
    57.  
    58.             if(RayCount == RayQuarterCount * 4 * 4 * 2){
    59.                 DrawPoints();
    60.             }
    61.         }
    62.     }
    63. }
    64.