Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

RayCast Shooting not working?

Discussion in 'Scripting' started by BubyMB, Jun 16, 2016.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys! I juts wrote a script, It's not working I do not know why. no errors or anything..

    just a simple shoot script that shoots towards the player if they have the Hittable tag
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Attack : MonoBehaviour
    6. {
    7.     public float fireRate = 3;
    8.     private float nextFire = 0.0F;
    9.     public int destroyTime = 1;
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    14.         {
    15.             Ray ray = new Ray(transform.position, transform.forward);
    16.             RaycastHit hit;
    17.             if (Physics.Raycast(ray, out hit))
    18.             {
    19.                 if (hit.collider.tag.Equals("Hittable"))
    20.                 {
    21.                     nextFire = Time.time + 2;
    22.                     GameObject clone = PhotonNetwork.Instantiate("projectile", transform.position, transform.rotation, 0) as GameObject;
    23.                     Rigidbody Temporary_RigidBody;
    24.                     Temporary_RigidBody = clone.GetComponent<Rigidbody>();
    25.                     Temporary_RigidBody.AddForce(transform.forward * 1000);
    26.                     clone.transform.LookAt(hit.collider.transform);
    27.                     StartCoroutine(destroyTimer(clone));
    28.                 }
    29.  
    30.             }
    31.  
    32.         }
    33.     }
    34.  
    35.     IEnumerator destroyTimer(GameObject clone)
    36.     {
    37.         yield return new WaitForSeconds(destroyTime);
    38.         PhotonNetwork.Destroy(clone);
    39.     }
    40. }
    41.  
    42.  
    I was just reading over the code, I was thinking maybe it is because it is not on the same plane?
     
    Last edited: Jun 16, 2016
  2. listener

    listener

    Joined:
    Apr 2, 2012
    Posts:
    179
    Does it execute everything ok but code after
    if(hit.collider.tag.Equals("Hittable")){...}
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Debug.DrawRay is your friend if you want to check what the ray is doing... as listener is hinting at, you need to put some Debug.logs in there to determine at what point it's going wrong.
     
  4. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Did that and it seems weird.. It draws a super short line the same length each time
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ah, transform.forward is "unit length" so the Debug.DrawRay is only going to be short, you can extend that (raycast takes its direction from the ray but without a given distance it defaults to infinity, drawray takes its direction and distance from the ray)...
     
  6. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    I don't really understand what you are saying? how would I draw it so it goes along with the proper line?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. Debug.DrawRay(transform.position, transform.forward * 10000);
    3.  
    or whatever arbitarily big number
     
  8. Coreystj

    Coreystj

    Joined:
    Sep 15, 2013
    Posts:
    78
    you must have a maximum cast length in order for your cast to find anything. I had issues with this in the past also...
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    only if you are trying to use a layer parameter, they're both numbers and thanks to how overloading works you can't "skip" an optional parameter if they happen to be the same type.
     
  10. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Okay, I am using a working script now;

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Attack : MonoBehaviour
    7. {
    8.     public float fireRate = 3;
    9.     private float nextFire = 0.0F;
    10.     public int destroyTime = 1;
    11.  
    12.     void Update()
    13.     {
    14.         RaycastHit hit = new RaycastHit();
    15.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.         if (Input.GetKeyUp(KeyCode.Mouse0))
    17.             if (Physics.Raycast(ray, out hit))
    18.             {
    19.                 if (hit.collider.tag == "Hittable" && Time.time > nextFire)
    20.                 {
    21.                     Debug.Log("Hit");
    22.                     transform.LookAt(hit.collider.transform);
    23.                     nextFire = Time.time + 2;
    24.                     GameObject clone = PhotonNetwork.Instantiate("projectile", transform.position, transform.rotation, 0) as GameObject;
    25.                     Rigidbody Temporary_RigidBody;
    26.                     Temporary_RigidBody = clone.GetComponent<Rigidbody>();
    27.                     Temporary_RigidBody.AddForce(transform.forward * 1000);
    28.                     clone.transform.LookAt(hit.collider.transform);
    29.                     StartCoroutine(destroyTimer(clone));
    30.                 }
    31.             }
    32.     }
    33.  
    34.     IEnumerator destroyTimer(GameObject clone)
    35.     {
    36.         yield return new WaitForSeconds(destroyTime);
    37.         PhotonNetwork.Destroy(clone);
    38.     }
    39. }
    40.  
    But now it doesn't shoot the players only normal objects i.e decorations
     
unityunity