Search Unity

Raycast not working

Discussion in 'Scripting' started by Micharneau, Aug 23, 2017.

  1. Micharneau

    Micharneau

    Joined:
    Dec 14, 2016
    Posts:
    11
    Hello everyone :
    I have a problem using Unity's Raycasting system : I want to draw a Ray between an enemy and the player. This way, if there are colliders between them, the enemy can't see the player.
    However, the raycast doesn't work. Here you have the concerned part of the script (In C#) :

    Code (CSharp):
    1.         RaycastHit hit;
    2.         if (Physics.Raycast (this.transform.position, playerTransform.transform.position, out hit)) {
    3.             if (hit.collider.tag == "Player") {
    4.                 playerIsVisible = true;
    5.             }
    6.         }
    And with this, I don't get any errors, but it simply doesn't work. Also, this part of the script is attached on the enemy.

    If what I provided is not enough to help resolve the problem, tell me and I'll give you further detail.

    Thank you in advance,
    Gaël "Nellorax" Bosc.

    N.B. : I'm French, so forgive me if I made any mistakes ^^
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What do you mean it doesn't work? What is or isn't happening? Is the code not running? Does it run but never finds the player?

    Also, how are you calling this method?
     
    Magiichan likes this.
  3. Micharneau

    Micharneau

    Joined:
    Dec 14, 2016
    Posts:
    11
    Thanks for the quick reply.
    When I say "It doesn't work", it means that the "playerIsVisible" variable never returns to true, despite the fact there are no objects between the player and the enemy, and the code is running fine. Also, I call the method using the Update fuction.
     
  4. Deleted User

    Deleted User

    Guest

    Where there should be vector math determining a direction between the player and another point there is instead just a gaping hole with the player's position.

    Go read Raycast's API. It does not work the same way as DrawLine or DrawRay.
     
  5. Deleted User

    Deleted User

    Guest

    Vector3 toPlayer = new Vectors3(somePosition - playerPosition)

    Raycast(someStartingPoint, someDirection, distance)

    DrawRay(someStartingPoint, someDirection)

    To draw the ray accurately, make someDirection a distance of 1, and multiply by the distance.

    DrawRay(someStartingPoint, someDirection.normalized * distance, color, duration)

    Make sure you get the range, .magnitude would be the exact distance between them, if you can make sqrMagnitude or an arbitrary value work that's better.

    In DrawRay you use the direction vector as the distance, in Raycast you declare a distance explicitly as one of the arguments and the direction vector is used only as a direction. You need to be able to convert back and forth between direction & distance and vector of a matching magnitude to draw raycasts accurately.

    Clarification. The second argument of the Raycast call is incorrect. For now make you objective to draw a single ray of accurate distance matching a raycast call that uses the if{}else{} to draw the ray in two different colors.
     
    Last edited by a moderator: Aug 23, 2017
  6. Deleted User

    Deleted User

    Guest

    Tonight I will write up an an academic example you can copy and test yourself and link my blog. This is a common point of confusion for newbies, I think. At least, I know it confused me.
     
  7. Micharneau

    Micharneau

    Joined:
    Dec 14, 2016
    Posts:
    11
    I finally found a solution !

    Code (CSharp):
    1.         RaycastHit hit;
    2.         Debug.DrawRay (this.transform.position, playerTransform.transform.position - this.transform.position, Color.green);
    3.         if (Physics.Raycast (this.transform.position, playerTransform.transform.position - this.transform.position, out hit)) {
    4.             First_Person_Character target = hit.transform.GetComponent<First_Person_Character>();
    5.             if (target != null) {
    6.                 playerIsVisible = true;
    7.             }
    8.         }
    This is a bit particular to explain, but using Debug.DrawRay, I noticed that the Raycast was moved as if I had written something like "(Physics.Raycast (this.transform.position, playerTransform.transform.position + this.transform.position, out hit))" so I substracted the player's position by the enemy's position, and it works just fine !

    Thanks to everyone !