Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem detection object with raycast

Discussion in 'Scripting' started by Kooth, Mar 2, 2015.

  1. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Hello , i have a little problem with my raycast.
    My raycast detected 1 out of 3 times my object correctly.
    What's wrong ?

    Code (JavaScript):
    1. var rayLenght : int = 10 ;
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8.  
    9. var hit : RaycastHit ;
    10. var fwd = transform.TransformDirection(Vector3.forward);
    11.  
    12.  
    13. if(Physics.Raycast(transform.position,fwd,hit, rayLenght)) {
    14.  
    15.     if(hit.collider.gameObject.tag == "a") {
    16.    
    17.        
    18.        
    19.         if(Input.GetKeyDown(KeyCode.E)  ) {  
    20.        
    21.         GetComponent(inventaire).a += 1 ;
    22.         Destroy(hit.transform.gameObject) ;
    23.         }          
    24.  
    25.         }
    26. }
    27.  
    28. }
    Thanks.
     
  2. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Maybe its related to this:

    You are casting the ray everyframe.

    IF it hits the a object, and u press E, then u do that code and destroy the a obj.

    Maybe, since frames are fast and u have to hit E at the right time, it fails sometimes.

    Maybe this could work:

    Create a private bool readyToPick, initialy set to false.

    When the ray hits the a object, this boll is set to true.

    In the update, if(readytTopick), then call a coroutine that waits for E to be clicked while readytTopick is true. If its false, leave.

    After picked, set readytTopick to false.

    Also, if raycast goes away from a, set readytTopick to false.

    Might fix it.
     
    Kooth likes this.
  3. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Thanks !
    My scripts can only be better.
    But not working totally :( There is a problem with the collision of my object.
    And this is strange but as soon as my objects spawn i manage to recover it but more my objects are old , more it's complicated to recover it.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var rayLenght : int = 10 ;
    4. var readytoPick : boolean ;
    5. function Start () {
    6. readytoPick = false ;
    7. }
    8.  
    9. function Update () {
    10.  
    11. var hit : RaycastHit ;
    12. var fwd = transform.TransformDirection(Vector3.forward);
    13.  
    14.  
    15.  
    16.  
    17. if(Physics.Raycast(transform.position,fwd,hit, rayLenght)) {
    18.  
    19.  
    20.     if(hit.collider.gameObject.tag == "a" && Input.GetKeyDown(KeyCode.E)) {
    21.        
    22.         readytoPick = true ;
    23.        
    24.        
    25.         if(readytoPick) {  
    26.    
    27.              
    28.        
    29.        
    30.        
    31.         GetComponent(inventaire).a+= 1 ;
    32.         Destroy(hit.transform.gameObject) ;
    33.        
    34.         readytoPick = false ;
    35.         }          
    36.  
    37.        
    38.    
    39.                    
    40.        
    41.        
    42.  
    43. }
    44.  
    45.  
    46. }
    47.  
    48.  
    49. }
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You should draw the ray/line in order to check whether it is casted correctly or not.
    Provide some more information about the object you want to hit with the raycast.
     
    Kooth likes this.
  5. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    try something like this - dont just copy the code, since i dont know javascript there must be errors, try to understand it. It should work.

    Code (JavaScript):
    1. #pragma strict
    2. var rayLenght : int = 10 ;
    3. var readytoPick : boolean ;
    4. function Start () {
    5. readytoPick = false ;
    6. }
    7. function Update () {
    8. var hit : RaycastHit ;
    9. var fwd = transform.TransformDirection(Vector3.forward);
    10. if(Physics.Raycast(transform.position,fwd,hit, rayLenght)) {
    11.     if(hit.collider.gameObject.tag == "a" )) {
    12.      
    13.         readytoPick = true ;
    14.        }
    15.     else { readytoPick = False;}
    16.  
    17. if(readytoPick) {
    18.  
    19.         if (!input.GetKeyDown(KeyCode.E) {
    20.        return;
    21.        } else {
    22.  
    23.      
    24.         GetComponent(inventaire).a+= 1 ;
    25.         Destroy(hit.transform.gameObject) ;
    26.      
    27.         readytoPick = false ;
    28.         }
    29.         }        
    30. }
    31.  
     
    Kooth likes this.
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you would be better to do it this way....

    Code (csharp):
    1.  
    2.  
    3. function Update() {
    4.  
    5. if(Input.GetKeyDown(KeyCode.E))
    6. {
    7.    var hit : RaycastHit ;
    8.    var fwd = transform.TransformDirection(Vector3.forward);
    9.  
    10.    if(Physics.Raycast(transform.position,fwd,hit, rayLenght))
    11.    {
    12.        if(hit.collider.gameObject.tag=="a"))
    13.        {
    14.           GetComponent(inventaire).a+=1;
    15.           Destroy(hit.transform.gameObject);
    16.       }
    17.     }
    18. }
    19. }
    20.  
    21.  
    Much more efficient code as its not constantly raycasting/transforming/etc. It only does it when the user hits the a key
     
    Kooth likes this.
  7. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Thanks for the post , i do that.
    My object is a cylinder. Indeed, on one side of the object, the raycast doesn't work..
    Screenshot : http://prntscr.com/6bz25a
     
  8. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Thanks guys to have replied to me I look your posts
     
  9. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Both codes super work well, thank you!
    I think it is from my object but I have no solution for fix it. :D
     
  10. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    you should use james code, as he said it only casts etc when required.
     
    Kooth likes this.
  11. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Hum ,How to get down the vector3.forward ?