Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Drawray bug

Discussion in 'Scripting' started by Eliecompteperso, Feb 28, 2020.

  1. Eliecompteperso

    Eliecompteperso

    Joined:
    Oct 9, 2019
    Posts:
    7
    Hi
    I am trying to make script for an enemie spaceship in a 3d space shooter.
    The enemi avoid obstacles by using therphysic.raycast method to cast 4 ray that allow it to know if it's going to collide with an object, I have used debug.drawray to see where the raycast are but the ray move away from their relativr position to the enemie when the enemie move . Here is the code for the ray:

    Vector3 left = transform.position + Vector3.left * rayOffset;
    Vector3 right =transform.position + Vector3.right * rayOffset;
    Vector3 up = transform.position + Vector3.up * rayOffset;
    Vector3 down = transform.position + Vector3.down * rayOffset;

    Debug.DrawRay(left, transform.forward * detectionDistance, Color.blue);
    Debug.DrawRay(right, transform.forward * detectionDistance, Color.blue);
    Debug.DrawRay(up, transform.forward * detectionDistance, Color. blue);
    Debug.DrawRay(down, transform.forward * detectionDistance, Color.blue);

    If someone can help me thank you in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    You are calculating the ray start positions by Vector3.left/right/up/down (world coordinate) offsets, but then you are raycasting with
    transform.forward
    , which is local coordinates, i.e, change depending on which way you're facing.

    NOTE: there is no
    transform.down
    , just use
    -transform.up
    . Same with left and back.
     
  3. Eliecompteperso

    Eliecompteperso

    Joined:
    Oct 9, 2019
    Posts:
    7
    I know that they will change direction depending on the way the object is facing that is intentional but my problem is that the rays also move slighly of their positioon relative to the object as the object move
     
  4. Eliecompteperso

    Eliecompteperso

    Joined:
    Oct 9, 2019
    Posts:
    7
    Ok i understand what you meant thank you very much

    Heres the code


    Vector3 left = transform.position - transform.right * rayOffset;
    Vector3 right =transform.position + transform.right * rayOffset;
    Vector3 up = transform.position + transform.up * rayOffset;
    Vector3 down = transform.position - transform.up * rayOffset;

    Debug.DrawRay(left, transform.forward * detectionDistance, Color.blue);
    Debug.DrawRay(right, transform.forward * detectionDistance, Color.blue);
    Debug.DrawRay(up, transform.forward * detectionDistance, Color. blue);
    Debug.DrawRay(down, transform.forward * detectionDistance, Color.blue);