Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

5.9 - 5.11 beta raycast + transform not updating bug?

Discussion in 'Unity 5 Pre-order Beta' started by melkior, Nov 4, 2014.

  1. melkior

    melkior

    Joined:
    Jul 20, 2013
    Posts:
    199
    Hello,

    I'm trying to do a raycast to determine line of sight between two game objects similar to the unity learn tutorial here:

    http://unity3d.com/learn/tutorials/projects/stealth/enemy-sight

    What happens is the first time you do a raycast it goes in the right direction.

    But once the 'player' object moves the following raycasts just go to the old location rather than the players new location.

    I suspect that this may be a Unity 5 bug regarding updating the transform location , can anyone review my code to see if it looks like I've done something wrong here?

    I put some debug statements in to show the raycasts to make it easy to see , and debug's to show where the object is expected to be also. I can upload a sample project if needed.

    You attach "RaycastEnemy" scripto to a 'monster' object and ToggleMove to a player object.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RaycastEnemy : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     public float fieldOfViewAngle = 180f;           // Number of degrees, centred on forward, for the enemy see.
    8.     public bool playerInSight = false;
    9.  
    10.     public void CheckLineOfSight()
    11.     {
    12.         // Create a vector from the enemy to the player and store the angle between it and forward.
    13.         Vector3 direction = player.transform.position - transform.position;
    14.  
    15.         Debug.Log("Rycast script thinks player is at" + player.transform.position);
    16.        
    17.         float angle = Vector3.Angle(direction, transform.forward);
    18.        
    19.         // If the angle between forward and where the player is, is less than half the angle of view...
    20.         if(angle < fieldOfViewAngle * 0.5f)
    21.         {
    22.             RaycastHit hit;
    23.  
    24.             Debug.DrawLine(transform.position, direction.normalized, Color.red, 3.0f);
    25.  
    26.             // ... and if a raycast towards the player hits something...
    27.             if(Physics.Raycast(transform.position, direction.normalized, out hit, 100f))
    28.             {
    29.                 // ... and if the raycast hits the player...
    30.                 if(hit.collider.gameObject == player)
    31.                 {
    32.                     // ... the player is in sight.
    33.                     playerInSight = true;
    34.                 } else {
    35.                     // the player is not in sight
    36.                     playerInSight = false;
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
    42.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ToggleMove : MonoBehaviour {
    5.  
    6.     public bool toggle = true;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         this.transform.position = new Vector3(0,0,0);
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     public void MoveToggler()
    19.     {
    20.  
    21.         if(!toggle)
    22.         {
    23.             toggle = true;
    24.             this.transform.position = new Vector3(0,0,0);
    25.             Debug.Log("Moved square to 0-0-0, actual is " + this.transform.position);
    26.  
    27.         } else {
    28.  
    29.             toggle = false;
    30.             this.transform.position = new Vector3(-3.5f,0,0);
    31.             Debug.Log("Moved square to -3.5,0,0, actual is " + this.transform.position);
    32.         }
    33.     }
    34. }
    35.  
     
  2. melkior

    melkior

    Joined:
    Jul 20, 2013
    Posts:
    199
    Last edited: Nov 5, 2014
  3. melkior

    melkior

    Joined:
    Jul 20, 2013
    Posts:
    199
    I did some posting around different places and on reddit someone had the answer.

    When changing code to remove the direction.normalized this now works.

    I'm totally unclear why that doesn't work because when you normalize it you are looking for the direction right? In any case I left my bug in because even this working solution at least has me wondering why the previous one did not work without a fully satisfactory answer.

    working code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RaycastEnemy : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     public float fieldOfViewAngle = 270f;           // Number of degrees, centred on forward, for the enemy see.
    8.     public bool playerInSight = false;
    9.  
    10.     public void CheckLineOfSight()
    11.     {
    12.         playerInSight = false;
    13.  
    14.         // Create a vector from the enemy to the player and store the angle between it and forward.
    15.         Vector3 direction = player.transform.position - transform.position;
    16.  
    17.         Debug.Log("Rycast script thinks player is at" + player.transform.position);
    18.        
    19.         float angle = Vector3.Angle(direction, transform.forward);
    20.        
    21.         // If the angle between forward and where the player is, is less than half the angle of view...
    22.         if(angle < fieldOfViewAngle * 0.5f)
    23.         {
    24.             RaycastHit hit;
    25.  
    26.             //Debug.DrawLine(transform.position, direction.normalized, Color.red, 3.0f);
    27.             Debug.DrawLine(transform.position, player.transform.position, Color.red, 3.0f);
    28.  
    29.             // ... and if a raycast towards the player hits something...
    30.             if(Physics.Raycast(transform.position, direction, out hit, 100f))
    31.             {
    32.                 // ... and if the raycast hits the player...
    33.                 if(hit.collider.gameObject == player)
    34.                 {
    35.                     // ... the player is in sight.
    36.                     playerInSight = true;
    37.                 } else {
    38.                     // the player is not in sight
    39.                     playerInSight = false;
    40.                 }
    41.             }
    42.         }
    43.     }
    44. }
     
  4. melkior

    melkior

    Joined:
    Jul 20, 2013
    Posts:
    199