Search Unity

[C#] Need help with getting raycast from another script

Discussion in 'Scripting' started by MichaelDShark, Nov 26, 2015.

  1. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    Hello
    So I have been trying to get a raycast to work.
    Basically I want it so when the raycast from my player weapon script
    hits ( collides ) with the AI it takes away the AI health from the AI health script.
    Thanks
    Michael

    This is the Player Weapon script(The one with the shooting raycast):

    public float fireRate = 0;
    public float Damage = 10;
    public LayerMask whatToHit;



    public Transform BulletTrailPrefab;
    public Transform MuzzleFlashPrefab;
    float timeToSpawnEffect = 0;
    public float effectSpawnRate = 10;
    public RaycastHit2D playerhit;

    float timeToFire = 0;
    public Transform firePoint;


    void Shoot () {
    Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    playerhit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
    if (Time.time >= timeToSpawnEffect) {
    Effect ();
    timeToSpawnEffect = Time.time + 1/effectSpawnRate;
    }
    Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
    if (playerhit.collider != null) {
    Debug.DrawLine (firePointPosition, playerhit.point, Color.red);

    }
    }

    This is the AI script (The one I want to the raycast to be in):
    using UnityEngine;
    using System.Collections;

    public class SpecOpUnitHealth : MonoBehaviour {


    public float health;

    private PlayerWeapon pWeapon;
    RaycastHit2D playerhit;
    Vector2 firepointPosition;



    // Use this for initialization
    void Start ()
    {



    }



    void TakeDamage( int damage)
    {
    pWeapon = GetComponent<PlayerWeapon> ();

    if (playerhit.collider)
    {
    Debug.Log("We hit the Spec Op Unit");
    }


    }
    }
     
    Last edited: Nov 26, 2015
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    We would require seeing your scripts to tell you exactly how to do that - why don't you upload the part of the shooting script (the method or function) where you do the raycast, then show use the part of the script where you handle taking away health, and maybe somebody can show you a way to hook em up.
     
  3. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    Ok doing that now should I just upload the screenshots ?
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Just copy and paste snippets from your code, be sure to use the code brackets so the formatting is right (the little buttons across the top of a new post allow you to do that, looks like a page icon sort of, three icons to the right of the smiley face)
     
  5. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    Did it :)
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Okay well, I guess I need more info now, first is, what happens right now when you hit the spec op unit? Does it draw a debug line showing it is noticing the collision with the raycast? And also, if that works, you need a way to identify what your hitting. So anyway, I am shooting blind with a bb gun at the moon here, lmao, but here is what I came up with for you to test out:

    First, in the playerscript try this - you will need to edit the name to match what you name your "spec op" characters in the scene, and might not need the "(Clone)" part if you don't instantiate them from a prefab.

    Code (CSharp):
    1. void Shoot()
    2.     {
    3.         Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    4.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    5.         playerhit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100, whatToHit);
    6.         if (Time.time >= timeToSpawnEffect)
    7.         {
    8.             Effect();
    9.             timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
    10.         }
    11.         Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
    12.         if (playerhit.collider != null)
    13.         {
    14.             Debug.DrawLine(firePointPosition, playerhit.point, Color.red);
    15.         }
    16.         // changes below
    17.         if(playerhit.collider.name == "SpecOpUnit(Clone)")
    18.         {
    19.             Debug.Log("Hit Spec Op Unit!");
    20.             SpecOpUnitHealth hitUnit = playerhit.collider.GetComponentInParent<SpecOpUnitHealth>() as SpecOpUnitHealth;
    21.             Debug.Log("Attempted to get unit script! Result: " + hitUnit); // if this shows null, it didnt work!
    22.             hitUnit.TakeDamage(Random.Range(15, 45)); // if all went well (doubt it) this will work
    23.         }
    24.         // changes above
    25.     }
    Then, in the spec op script try this -

    Code (CSharp):
    1. public void TakeDamage(int damage)
    2.     {
    3.         pWeapon = GetComponent<PlayerWeapon>(); // you should have a reference to this rather than "getting" it each time
    4.  
    5.         if (playerhit.collider)
    6.         {
    7.             Debug.Log("We hit the Spec Op Unit");
    8.             // here you should lower health (for example:)
    9.             //health = health - Random.Range(15, 45); // randomly remove between 15 and 45 health from this hit
    10.             // (or since you hand off damage to the method)
    11.             health = health - damage;
    12.             if(health <= 0)
    13.             {
    14.                 Debug.Log("I should be dead!");
    15.             }
    16.         }
    17.     }
    Let me know what goes wrong, as I can't test it. Good luck!
     
  7. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    When the raycast his the Spec Op , it does not say the console message.
    Debug.Log("Attempted to get unit script! Result: " + hitUnit); // if this shows null, it didnt work!
    That messaged is coming up.
    Also how would I make the reference sorry I'm knew cause this warning keeps on coming up
    that its value is never used.
    Thanks
    Michael
     
    Last edited: Nov 28, 2015
  8. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Sorry been busy working on a new project - I would suggest you debug a little, stick some debug statements in there and see how far through the code it is working as expected, then when you find where things are going wrong report back and I'll try and come up with something. Without physically seeing your project there is only so much guessing I can do, so you'll have to do some of the legwork, and try and get to the bottom of this.
     
  9. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    Ok , whats your project ?
    Could I send you my project ?
    Cause I really want to get this part of my game done I have been trying to do it for two weeks
    How would I reference pWeapon?
    Thanks
    Michael
     
  10. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I am sorry I don't have the spare time right now to look it over - but I bet somebody would be able to do that if you upload the project (if you don't mind sharing anyway). I am not sure how pWeapon is handled so like I said without looking at it I couldn't tell ya, sorry!
     
  11. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    Ok maybe I don't need pWeapon
    upload it where ?
    Know any Youtubers or videos which could me learn this type of thing ?
    Thanks
    Michael
     
  12. MichaelDShark

    MichaelDShark

    Joined:
    Apr 11, 2015
    Posts:
    45
    Edited the script , got it working ,
    now just working on making the AI lose health on hit.
    Thanks for the help
    Michael