Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

I seem to have a RaycastHit problem?

Discussion in 'Scripting' started by OnlySpace_unity, Aug 1, 2020.

  1. OnlySpace_unity

    OnlySpace_unity

    Joined:
    Aug 1, 2020
    Posts:
    5
    So im an aspiring programer and im trying to figure out what the ever living Fudge buckets is going on with this code...

    so this turret script finds an enemy, calls the Lazer script which uses a raycast and a line renderer to make a laser, wich is working because it spawns an explosion on the target collider where it hit, but im also trying to get the laser to tell the object(whatever it is) to take damage and that is in fact not happening and i think it was fine for half a second today, but i may have just been delerious...

    relevent Code below


    [SerializedField] public int smallLaserDamage = 1;

    Vector3 CastRay()
    {
    RaycastHit hit;

    Vector3 fwd = transform.TransformDirection(Vector3.forward) * maxDistance;

    if (Physics.Raycast (transform.position, fwd, out hit))

    {
    SpawnExplosion(hit.point, hit.transform)

    FighterSheilds health = hit.collider.GetComponent<FighterSheilds>();

    if ( health != null)
    {
    health.fighterTakeDamage (smallLazerDamage);
    }

    .....


    }

    then the other script is like


    public void FighterTakeDamage (int damageammount)
    {
    currHealth -= damageammount;

    ...

    if theres anyhting i left out that you think is important, feel free please!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    2,398
    In the future please use code tags as described here to make the code in your post more readable: https://forum.unity.com/threads/using-code-tags-properly.143875/

    As far as your code - from what I'm seeing here it looks fine to me. Can you explain what makes you think it's not working? have you tried adding Debug.Log statements in your code at strategic places to see which code is running and when? Maybe log which object you hit and how much damage you're subtracting from their health, and their health before and after doing that.
     
  3. OnlySpace_unity

    OnlySpace_unity

    Joined:
    Aug 1, 2020
    Posts:
    5
    1. my bad, ive never used a forum before or anything
    2. well the object that its hitting, isnt getting anything subtracted from its health. a hundered lasers hit the ship at once, and the ship has a rigid body and a two colliders just to be safe, and nothing.

    i have a missile weapon system that uses "OnTriggerEnter (Collider other)" and it does exactly the ammount of damage it is said to do, and debugs when it both hits and destroys the enemy... all the sources ive read and seen, im doing everything right, but i know im missing something...
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    2,398
    How did you verify that the health is not being subtracted? Are you looking at a value in an inspector? Which inspector? Are you reading a Debug.Log message?

    Are you sure that the thing you're hitting has a FighterSheilds component? Based on your code an xplosion will still be spawned but nothing else will happen if the object doesn't have that component. Have you tried logging whether or not that component exists on the object you hit, and/or the name of the object you hit?
     
  5. OnlySpace_unity

    OnlySpace_unity

    Joined:
    Aug 1, 2020
    Posts:
    5
    Each spawned fighter is spawned via a prefb which has all the scripts and the links connecting them.
    each hit on the fighter does spawn an explosion, called from the laser script, and the debug says it hit, some disconnect between the doing damage part, and the removing health part

    im watching each fighter that is targeted from the inspector in the scene window. i can also watch the health drop when i shoot anything with the missles or machineguns but not the lazers because they are using raycasting instead of simple collision detection.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    2,398
    Out of curiosity, what does the missile collision code look like? Isn't it similar? Grabbing the FighterSheilds component from the collider you hit and calling the same method on it?
     
  7. OnlySpace_unity

    OnlySpace_unity

    Joined:
    Aug 1, 2020
    Posts:
    5
    yup!

    void OnTriggerEnter(Collider other)
    {

    ...
    other.gameObject.GetComponentt<FighterSheilds>() . FighterTakeDamage(smallmissiledamage);

    ...

    same principal, but i guess its actually the one doing the damage wheras the laser as a turret script that also does the targeting, and is also attatched to the explosion script... idk, i would just copy the code over but its on my wifi-less computer
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    2,398
    Is the FighterTakeDamage method actually running?

    Is it possible that this field got set to 0 in the inspector?

    [SerializedField] public int smallLaserDamage = 1;


    Then maybe everything is working but you're just subtracting 0 by accident, so it does nothing?
     
  9. OnlySpace_unity

    OnlySpace_unity

    Joined:
    Aug 1, 2020
    Posts:
    5
    all values are perfectly fine!

    maybe its the
    "FighterSheilds health =" part because it actually looks like :

    FighterSheilds health = hit.collider.GetComponent<FighterSheilds>();

    Sheild playerHealth = hit.collider.GetComponent<Sheild>();

    FrigateHull frigateHull = hit.collider.GetComponent<FrigateHull>;

    if ( health != null)
    {
    health.fighterTakeDamage (smallLazerDamage);
    Debug.Log("damage dealt")
    }
    else

    if ( playerHealth != null)
    {
    playerHealth.TakeDamage (smallLazerDamage);
    }

    else

    if ( frigateHull != null)
    {
    frigateHull.frigateDamage (smallLazerDamage);
    }

    return hit.point.
     
unityunity