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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Cast Rays to check collisions in Front of a GameObject?

Discussion in 'Scripting' started by lisandroguevara210, Oct 30, 2018.

  1. lisandroguevara210

    lisandroguevara210

    Joined:
    Oct 30, 2018
    Posts:
    5
    Hi! I have a Rail Shooter (House of the Dead-Like), and i Have an issue.
    As I have multiple enemies that aproach the static player using NavMesh and NavMeshAgent, I need to make them stop if they have another enemy in front of them, as they will constantly push eachother to get through.
    I tried several things as BoxCast, ShpereCast, CheckShpere.... but they dont work as I detroy the enemies when they get shot, so any OnTriggerExit wont work.
    My final idea is to cast several Rays from the enemy to his front to see if any other enemy is there, and tell him to stop moving until there's none. So I must cast Rays in different ANGLES at the same time.
    My problem is: how do i change the Ray's angle?
    I tried:

    Code (CSharp):
    1.         RaycastHit hit1;
    2.         var direction1 = transform.forward;
    3.         Physics.Raycast(transform.position, direction1, out hit1, 5);
    4.  
    5.         RaycastHit hit2;
    6.         var direction2 = transform.forward*Quaternion.AngleAxis(30,transform.forward);
    7.         Physics.Raycast(transform.position, direction2, out hit1, 5);

    But is says i cant MULTIPLY Vector3 * Quaternion.

    Any help or ideas would be appreciated.

    Thank you!

    Lisandro, from Argentina.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Code (CSharp):
    1. var direction2 = transform.forward*Quaternion.AngleAxis(30,transform.up);
    Rotate forward around up axis by 30 degrees.
    (Rotate forward vector around itself yields no difference, output still is forward)
     
  3. lisandroguevara210

    lisandroguevara210

    Joined:
    Oct 30, 2018
    Posts:
    5
    Oh yes right, it should be transform.up.
    Nevertheless Visual Studio Says I cant multiply Vector3 *(per) Quaternion.:(
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    It's the other way around, sorry.
    Quaternion * Vector3
     
  5. lisandroguevara210

    lisandroguevara210

    Joined:
    Oct 30, 2018
    Posts:
    5
    Right! I will try this and let the Fourm know! Be right back!

    Thank you Thermal!
     
  6. lisandroguevara210

    lisandroguevara210

    Joined:
    Oct 30, 2018
    Posts:
    5
    Edit: It works but it detects collision all the time, wich stops all my enemies... thats wierd because i only cast 7 rays in these angles: 0, 10, 20, 30, -10, -20 and -30.

    Also when I draw the line it draws a line in the bottom of the object (the ground)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RayCreator : MonoBehaviour {
    6.  
    7.     Animator animatorEnemy;
    8.     EnemyMovement enemy;
    9.     LayerMask layermaskEnemy = ~10;
    10.     Vector3 rayPos;
    11.  
    12.     void Start ()
    13.     {
    14.         animatorEnemy = GetComponent<Animator>();
    15.         enemy = GetComponent<EnemyMovement>();
    16.         rayPos = new Vector3(transform.position.x+0.35f, transform.position.y + 1, transform.position.z);
    17.  
    18.     }
    19.    
    20.     void Update ()
    21.     {      
    22.         RaycastHit hit1;
    23.         var direction1 = transform.forward;
    24.       //Physics.Raycast(transform.position, direction1, out hit1, 3,layermaskEnemy);
    25.         RaycastHit hit2;
    26.         var direction2 = Quaternion.AngleAxis(10, transform.up)*transform.forward;
    27.        //Physics.Raycast(transform.position, direction2, out hit2, 3, layermaskEnemy);
    28.         RaycastHit hit3;
    29.         var direction3 = Quaternion.AngleAxis(20, transform.up) * transform.forward;
    30.        //Physics.Raycast(transform.position, direction3, out hit3, 3, layermaskEnemy);
    31.         RaycastHit hit4;
    32.         var direction4 = Quaternion.AngleAxis(30, transform.up) * transform.forward;
    33.        //Physics.Raycast(transform.position, direction4, out hit4, 3, layermaskEnemy);
    34.         RaycastHit hit5;
    35.         var direction5 = Quaternion.AngleAxis(-10, transform.up) * transform.forward;
    36.        //Physics.Raycast(transform.position, direction5, out hit5, 3, layermaskEnemy);
    37.         RaycastHit hit6;
    38.         var direction6 = Quaternion.AngleAxis(-20, transform.up) * transform.forward;
    39.         //Physics.Raycast(transform.position, direction6, out hit6, 3, layermaskEnemy);
    40.         RaycastHit hit7;
    41.         var direction7 = Quaternion.AngleAxis(-30, transform.up) * transform.forward;
    42.         //Physics.Raycast(transform.position, direction7, out hit7, 3, layermaskEnemy);
    43.        
    44.         if (Physics.Raycast(rayPos, direction1, out hit1, 4, layermaskEnemy) || Physics.Raycast(rayPos, direction2, out hit2, 4, layermaskEnemy) ||
    45.            Physics.Raycast(rayPos, direction3, out hit3, 4, layermaskEnemy) || Physics.Raycast(rayPos, direction4, out hit4, 4, layermaskEnemy) ||
    46.            Physics.Raycast(rayPos, direction5, out hit5, 4, layermaskEnemy) || Physics.Raycast(rayPos, direction6, out hit6, 4, layermaskEnemy) ||
    47.            Physics.Raycast(rayPos, direction7, out hit7, 4, layermaskEnemy))
    48.         {
    49.             Debug.DrawRay(transform.position, transform.forward, Color.green);
    50.             Debug.Log("CHOQUE CONTRA ENEMIGO");
    51.             animatorEnemy.SetFloat("Speed", 0);
    52.             enemy.isStopped = true;
    53.             return;
    54.         }
    55.         else
    56.         {
    57.             Debug.DrawRay(transform.position, transform.forward, Color.green);
    58.             animatorEnemy.SetFloat("Speed", 0.1f);
    59.             enemy.isStopped = false;
    60.         }
    61.     }
    62. }
     
    Last edited: Oct 30, 2018
  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You should really replace all that duplication with a loop.
    It's hard to tell exactly what happens from the code. I see you create a rayPos at start, but I can't see you update it.
    Your debug rays also don't start at rayPos but the raycasts do.
    Make sure the rays don't start in the ground or hit their own enemy.
     
  8. lisandroguevara210

    lisandroguevara210

    Joined:
    Oct 30, 2018
    Posts:
    5
    upload_2018-11-4_15-39-20.png

    I think this is the problem, the ray touches de enemy where its origin is, so the enemy stops when it shouldn't.
    I added the rayPos line in the Update so it changes every frame but this still happens

    The weird thing is that in rayPos i wrote that it should put the ray's origin in transfrom.position.x+0.35f and it does not. I even tried changing that number to bigger ones like transfrom.position.x+10 but the ray keeps originating at the same spot... help!!