Search Unity

How can I make the raycasts directions in the same distances from each other when the player turn?

Discussion in 'Scripting' started by hamik166, Oct 19, 2019.

  1. hamik166

    hamik166

    Joined:
    Oct 19, 2019
    Posts:
    31
    This is my code:


    Code (CSharp):
    1. RaycastHit tmoHit;
    2.             Physics.Raycast(transform.position, transform.forward + new Vector3(0.3f, 0, 0), out tmoHit, 15);
    3.             Debug.DrawLine(transform.position, tmoHit.point,Color.blue);
    4.             Physics.Raycast(transform.position, transform.forward + new Vector3(0.6f, 0, 0), out tmoHit, 15);
    5.             Debug.DrawLine(transform.position, tmoHit.point, Color.blue);
    6.             Physics.Raycast(transform.position, transform.forward , out tmoHit, 15);
    7.             Debug.DrawLine(transform.position, tmoHit.point, Color.blue);
    What I expected it to do was to make 3 lines that will come out of my player with the same distance between them.

    but what actually happens is that the distance/offset between them changes, and sometimes they are going to the same direction, but as ou can see in my code, I set a diffrent direcration for each one of them, how can I fix it?



    here is a video showing the problem:


    [
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome.

    Let's have a look at a couple examples
    (note that those are not actual forward values, as those would be normalized but i chose easy values for the examples):
    Example 1: transform.forward = (1,0,0), your directions are (1,0,0), (1.3,0,0) and (1.6,0,0)
    Example 2: transform.forward = (0.5, 0, 0.5), your directions are (0.5, 0, 0.5), (0.8, 0, 0.5), (1.1, 0, 0.5)
    Example 3: transform.forward = (0.1, 0, 0.9), your directions are (0.1, 0, 0.9), (0.4, 0, 0.9), (0.7, 0, 0.9)
    Try imagining the vectors on a 2D coordinate system (ignoring y for now).

    upload_2019-10-19_1-42-33.png

    Now imagine you draw a line from the origin, through each of these 3 dots. As you may imagine, the lower the z-value of your direction, the closer the lines will get. In the first example, where z = 0, the lines will not only be parallel, but go in the same direction. This is similar to what happens in your game right now.

    To change this, you also need to account for changes to the z value. However, as you probably figured out, if you add the same part to z as you do to x, then the lines will go in the same direction all the time (dots in the coordinate system above would form a diagonal line). Instead, what you want to do is to use transform.forward as the middle one of your directions and add your values to it, then normalize the direction. This makes it so that the total length of the vector is 1, thus it nets you a location on the "unit circle".

    What you probably want to do, to both make it easier understandable and more precise, is define your rays in angles. This can be done rather easily. For an example see this post, especially the last answer for custom angles.
    https://answers.unity.com/questions/146975/how-to-raycast-on-45-degree.html

    Hope this helps! :)
     
    Last edited: Oct 19, 2019
    nilsdr, hamik166 and orenog like this.
  3. orenog

    orenog

    Joined:
    Jan 9, 2016
    Posts:
    9
    Thank you so muuuuchhhh!!!! Reallllllyyy thankkkkkkssss you helped me so much!!! THANK YOU!! THANK YOU!@!!!!!!!
     
  4. orenog

    orenog

    Joined:
    Jan 9, 2016
    Posts:
    9
    it really helped, but now I tried around another exis and I got another problem, this is how it looks now:


    and this is my new code:
    Code (CSharp):
    1.  
    2.         Physics.Raycast(transform.position, Quaternion.AngleAxis(5, transform.right) * transform.forward, out tmoHit, 15);
    3.         Debug.DrawLine(transform.position, tmoHit.point, Color.blue);
    4.         Physics.Raycast(transform.position, Quaternion.AngleAxis(10, transform.right) * transform.forward, out tmoHit, 15);
    5.         Debug.DrawLine(transform.position, tmoHit.point, Color.blue);
    6.         Physics.Raycast(transform.position, Quaternion.AngleAxis(-5, transform.right) * transform.forward, out tmoHit, 15);
    7.         Debug.DrawLine(transform.position, tmoHit.point, Color.blue);
    8.         Physics.Raycast(transform.position, Quaternion.AngleAxis(-10, transform.right) * transform.forward, out tmoHit, 15);
    9.         Debug.DrawLine(transform.position, tmoHit.point, Color.blue);
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    May i ask what it is you are trying to achieve? As in, why / what for do you need those raycasts? What information do you try to get, and why is that important? Maybe there is an easier solution to your problem.

    As for your actual problem; i had to do some testing, but it appears that this is simply a problem with the way you draw your lines. You draw the line from your position to the hit position. If the ray misses all targets, it returns (0,0,0) as hit, which results in a line being drawn from your rover to the origin. The ray should go in the intended direction, it just misses the wall and returns no hit location.
     
  6. orenog

    orenog

    Joined:
    Jan 9, 2016
    Posts:
    9
    Thank you so much, it was actully the problem, I am the stupidest person alive
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    No, these things happen. I didnt spot the problem at first either, so i reproduced the scene and printed some values to get a better idea. On that note: Debug.Log() is your friend. I first printed the ray direction, which didnt give me any insight. Then i printed the hit position, which basically explained the problem.
    Just glad that my scene (wall height) coincidentally had the same problem as yours haha. Even tho, if i didnt encounter the problem that would probably lead to a similar conclusion, since it then must be something outside of the code. So the point of this post is: try to collect new information about any problem. The more you know (when it occurs, when it does not, ...) the easier it becomes to fix. What may seem like an unexplainable bug often has a simple explanation - even tho it does not have to be easy to find.