Search Unity

Raycast will not work when a rigidbody object is applied in 2019.1

Discussion in 'Physics' started by PhoenixAdvanced, Jun 29, 2019.

  1. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hello,

    I have just run into a bizarre issue which I have yet to get to the bottom of.

    I noticed that my raycasts were failing to pick up an object, and I realised that it was the rigidbody object on the object that was preventing the raycast from working.

    When the object has a box collider only, raycasting works.
    When the object has a box collider and a rigidbody, raycasting fails.

    This did not occur in previous versions of unity, so it seem that something must have changed in 2019.1.

    This is my raycast code:

    Code (CSharp):
    1.  
    2.        if (Physics.Raycast(eye, out hit, range))
    3.             {
    4.                     Debug.Log("HIT :> " +hit.transform.name + " " + hit.collider.name);
    5.  
    Can anyone offer advice on this?

    I have even created a very simple test case:
    Create two cubes.
    Attach a rigidbody to one cube.

    The cube without the rigidbody will be detected by the above code. The one with the rigidbody won't be.

    Remove the rigidbody from the second cube. Now it's detected.

    I can't understand this behaviour?
     
  2. Deleted User

    Deleted User

    Guest

    Maybe you should post the entire script? How do you define:
    • eye,
    • hit,
    • range?
     
  3. Deleted User

    Deleted User

    Guest

    Btw, I do not have this problem in my 2D project. Physics.Raycast detects the ground and it has both a collider and a rigidbody.
     
  4. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Thanks for your reply.

    I suspect this must be something that I have done in my project, but eye hit and range are just defined as:

    Ray eye = cam.ScreenPointToRay(Input.mousePosition);

    and then as globals:
    private RaycastHit hit;
    private float range = 300;

    I can't think of what else could be causing this.
     
  5. Deleted User

    Deleted User

    Guest

    You need to post the entire script.
     
  6. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hey, thanks again.

    I think I got this figured out. I had an object that was invisible in the scene but had a collision box, which was somehow blocking the rays when the rigidbody was attached, but not otherwise.

    I'm not sure why it was selectively blocking the rays like that, but it was definitely caused by that object.
     
  7. maomaothegreat1

    maomaothegreat1

    Joined:
    Jul 6, 2023
    Posts:
    22
    I dont know if anyone is still on this thread but i am having the same problem. everything works perfectly till i add a rigid body to the hit obj.
    plz excuse the slightly unorganized Code i have not gotten to cleaning it up.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class ProjectileScript : MonoBehaviour
    {
    Rigidbody rb;
    public bool isBullet;
    public bool isRocket;
    public bool isBomb;
    public bool Fired;
    public int RoundType;
    public float Damage;
    public float HEDamage;
    public float Velo;
    public float FuseTime;
    public float MaxLifeTime;
    public float LifeTime;
    public float Fule;
    public float TransTemp;
    public float Penitration;
    public float SpredDistance;
    public int Cluster;
    public float ExplosiveRange;
    public float FrieRange;
    public float FrieStrength;
    public float FrieTime;
    public float SlowRange;
    public float SlowedVelo;
    public bool Slowed;
    public float Bulletlength;
    public bool Loaded;
    public bool Firing;
    public bool Armed;
    public LayerMask LayersHit;
    public float BarelTime;
    public float Rounds;
    public bool TestBool;
    public float TestValue;
    public GameObject MagRound;
    public bool IsLoading;
    public bool BaseRound;
    public Transform ProjectileTransform;
    public Transform ProjectileTransformR;
    // Start is called before the first frame update
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    rb.isKinematic = true;
    }
    // Update is called once per frame
    void Update()
    {
    if (IsLoading==true)
    {
    rb.AddForce(transform.forward * 0.1f, ForceMode.Impulse);
    IsLoading = false;
    }
    if (LifeTime <= MaxLifeTime- BarelTime && Armed==false)
    {
    Armed = true;
    }
    if (Fired == false)
    {
    if (ProjectileTransform != null&&Loaded==true)
    {
    transform.position = ProjectileTransform.position;
    transform.rotation = ProjectileTransformR.rotation;
    }
    }
    if (Fired==true)
    {
    //BULLET STUFF////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isBullet)
    {
    CheckHit();
    LifeTime -= Time.deltaTime;
    if (LifeTime <= 0)
    {
    Death();
    }
    }
    //BOMB STUFF////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isBomb)
    {
    }
    //Rocket STUFF////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isRocket)
    {
    }
    }
    }
    void Modify (float Damage,float HEDamage,float Velo,float FuseTime,float LifeTime,float Fule,float TransTemp,float Penitration,float SpredDistance,int Cluster,float ExplosiveRange,float FrieRange,float FrieStrength,float FrieTime)
    {
    if (Armed==true)
    {
    }
    }
    void CheckHit ()
    {
    RaycastHit hit;
    if (Armed == true)
    {

    if (Physics.Raycast(transform.position, Vector3.forward, out hit, SlowRange, LayersHit))
    {
    if (Slowed == false)
    {
    rb.AddForce(transform.forward * SlowedVelo * -1, ForceMode.Impulse);
    Slowed = true;
    }
    }
    if (Physics.Raycast(transform.position, transform.forward, out hit, (.15f + Bulletlength), LayersHit))
    {
    float Angle = Vector3.Angle(transform.forward, hit.normal);
    TestValue = Angle;
    Vector3 bounceDirection = Vector3.Reflect(transform.forward, hit.normal);
    BasicParrtScript basicParrtScript = hit.transform.GetComponent<BasicParrtScript>();
    if (basicParrtScript != null)
    {
    if (basicParrtScript.Armor > Penitration / (180 / Angle))
    {
    TestBool = true;
    basicParrtScript.TakeDamage(0, 0, TransTemp, 0, 0, Angle);
    rb.AddForce(bounceDirection * Velo, ForceMode.Impulse);
    }
    }
    if (basicParrtScript != null)
    {
    if (basicParrtScript.Armor < Penitration / (90 / Angle))
    {
    basicParrtScript.TakeDamage(Damage, Penitration, TransTemp, HEDamage, ExplosiveRange, Angle);
    Death();
    }
    if (basicParrtScript.Armor / 2 < Penitration / (90 / Angle) && basicParrtScript.TempC >= basicParrtScript.WeakTemp)
    {
    basicParrtScript.TakeDamage(Damage, Penitration, TransTemp, HEDamage, ExplosiveRange, Angle);
    Death();
    }
    }
    }
    }
    }
    void Death()
    {
    if (Cluster>0)
    {
    }
    Destroy(gameObject);
    }
    public void Fire ()
    {
    rb.isKinematic = false;
    Fired = true;
    LifeTime = MaxLifeTime;
    rb.AddForce(transform.forward * Velo, ForceMode.Impulse);

    }
    private void OnDrawGizmosSelected()
    {
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(transform.position, Bulletlength);
    Gizmos.color = Color.yellow;
    Gizmos.DrawWireSphere(transform.position, (.15f + Bulletlength));
    }
    public void Destroy ()
    {
    Destroy(gameObject);
    }
    }
     

    Attached Files:

  8. maomaothegreat1

    maomaothegreat1

    Joined:
    Jul 6, 2023
    Posts:
    22
    though i should also add the hit objects script


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class BasicParrtScript : MonoBehaviour
    {
    public float Armor;
    public float MaxHp;
    public float Weight;
    public float HPC;
    public float WeakTemp;
    public float TempC;
    public float BaseCamo;
    public float BaseCamoC;
    public float MaoingtonValue;
    public GameObject SpecialFunctioningObject;
    public bool OnFire;
    public bool CanHeal;
    public float Filler;
    public float BurnTime;
    public float TimeBurning;
    public float CalcPen;
    // Start is called before the first frame update
    void Start()
    {
    HPC = MaxHp;
    }
    // Update is called once per frame
    void FixedUpdate()
    {
    if (BurnTime > 0 && OnFire)
    {
    BurnTime = BurnTime -= Time.deltaTime;
    TimeBurning += Time.deltaTime;
    }
    }
    void Update()
    {
    if (BurnTime > 0) { OnFire = true; }
    if (HPC < MaxHp)
    {
    CanHeal = true;
    }
    if (HPC > MaxHp)
    {
    CanHeal = false;
    HPC = MaxHp;
    }
    if (HPC == MaxHp)
    {
    CanHeal = false;
    }
    if (OnFire == true)
    {
    Burning();
    }
    }
    public void TakeDamage(float Damage,float Penitration,float TransTemp,float HEDamage, float ExplosiveRange,float Angle)
    {
    if (Angle > 0)
    {
    CalcPen = Penitration / (90 / Angle);
    }
    if (Angle == 0)
    {
    CalcPen = Penitration;
    }
    //FiredBool = SpecialFunctioningObject.GetComponent<SpecialFunctionScript>.Fired;
    TempC = TempC + TransTemp;
    //allDeath= SpecialFunctioningObject.GetComponent<SpecialFunctionScript>.Death();
    if (Penitration > Armor && TempC < WeakTemp)
    {
    //SpecialFunctioningObject.GetComponent<SpecialFunctionScript>.Hit();
    HPC -= (Damage - (Penitration / Armor));
    HPC -= HEDamage;
    }
    if (Penitration > Armor/2 && TempC >= WeakTemp)
    {
    //SpecialFunctioningObject.GetComponent<SpecialFunctionScript>.Hit();
    HPC -= (Damage+Damage/4 - (Penitration / Armor));
    HPC -= HEDamage;
    }
    if (HPC < 0)
    {
    /*if (OnFire)
    {
    FiredBool = true;
    callDeath;
    }
    if (HEDamage > 0)
    {
    FiredBool = true;
    callDeath;
    }
    if (HEDamage <= 0)
    {
    FiredBool = false;
    callDeath;
    }*/
    }
    }
    void Heal (float HealStrength)
    {
    if (CanHeal)
    {
    HPC += HealStrength;
    }
    }
    void Burning()
    {
    CanHeal=false;
    HPC -= 1 * (TimeBurning / 30);
    if (BurnTime<=0)
    {
    OnFire = false;
    }
    }
     
  9. maomaothegreat1

    maomaothegreat1

    Joined:
    Jul 6, 2023
    Posts:
    22
    im using unity 2022.3.4f1
     
  10. a_karpins

    a_karpins

    Joined:
    Mar 8, 2023
    Posts:
    9
    I have the same issue when using XRRayInteractor.TryGetCurrent3@RayCastHit(out hit), however the issue is only with dynamic rigidbodies, kinematic seems to work fine. Using Unity version 2023.2.0b14