Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KeskitaloStudios · Jun 12, 2016 at 04:32 AM · raycastshootingraycastingrangelength

How to make raycast range on this script?

using UnityEngine; using System.Collections;

public class PlayerShooting : MonoBehaviour {

 public string weaponname = "PickAxe";
 public float weapondamage = 10f;
 public float weaponrange = 1f;
 public float cooldown = 0f;
 public float firerate = 1f;

 FXManager fxmanager;
 [SerializeField]
 private Camera cam;
 void Start()
 {
     fxmanager = GameObject.FindObjectOfType<FXManager>();

     if (fxmanager == null)
     {
         Debug.LogError("Cant find FX Manager");
     }
 }

   
 void Update()
 {
     cooldown -= Time.deltaTime;

     if (Input.GetButton("Fire1"))
     {
         Fire();
     }

 }

 void Fire()
 {
     if (cooldown > 0)
     {
         return;
     }
     

     Ray ray = new Ray(cam.transform.position, cam.transform.forward);
     Transform hitTransform;
     Vector3 hitPoint;

     hitTransform = FindClosestHitObject(ray, out hitPoint);

     if (hitTransform != null)
     {
         Debug.Log("We hit: " + hitTransform.name);



         Health h = hitTransform.GetComponent<Health>();

         while (h == null && hitTransform.parent)
         {
             hitTransform = hitTransform.parent;
             h = hitTransform.GetComponent<Health>();
         }



         if (h != null)
         {
             PhotonView pview = h.GetComponent<PhotonView>();
             if (pview == null)
             {
                 Debug.LogError("NO PHOTONVIEW ATTACHED");
             }
             else
             {
                 GroupMember GMember = hitTransform.GetComponent<GroupMember>();
                 GroupMember myGMember = this.GetComponent<GroupMember>();
                 if (GMember == null || GMember.teamID == 0 || myGMember == null || myGMember.teamID == 0 || GMember.teamID != myGMember.teamID)
                 {
                     h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, weapondamage);
                 }
             }

         }
         if (fxmanager != null)
         {
             fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
         }

     }
     else
     {
         if (fxmanager != null)
         {
             hitPoint = cam.transform.position + (cam.transform.forward * weaponrange);
             fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
         }
     } 
 
     cooldown = firerate;
 }

 Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint)
 {

     RaycastHit[] hits = Physics.RaycastAll(ray);

     Transform closestHit = null;
     float distance = 0;
     hitPoint = Vector3.zero;

     foreach (RaycastHit hit in hits)
     {
         if (hit.transform != this.transform && (closestHit == null || hit.distance < distance))
         {
     

             closestHit = hit.transform;
             distance = hit.distance;
             hitPoint = hit.point;
         }
     }


     return closestHit;

 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KeskitaloStudios · Jun 11, 2016 at 10:17 AM 0
Share

Fixed! dropping off the code that i used to fix it if someone else needs it :) void Fire() { if (cooldown > 0) { return; }

     Ray ray = cam.ScreenPointToRay(Input.mousePosition); //these are the changes made
     RaycastHit hit;//these are the changes made
     if (Physics.Raycast(ray, out hit, weaponrange))//these are the changes made
     {
         Debug.DrawLine(ray.origin, hit.point);//these are the changes made
     }
     Transform hitTransform;
     Vector3 hitPoint;

     hitTransform = FindClosestHitObject(ray, out hitPoint);

     if (hitTransform != null)
     {
         Debug.Log("We hit: " + hitTransform.name);



         Health h = hitTransform.GetComponent<Health>();

         while (h == null && hitTransform.parent)
         {
             hitTransform = hitTransform.parent;
             h = hitTransform.GetComponent<Health>();
         }



         if (h != null)
         {
             PhotonView pview = h.GetComponent<PhotonView>();
             if (pview == null)
             {
                 Debug.LogError("NO PHOTONVIEW ATTACHED");
             }
             else
             {
                 Group$$anonymous$$ember G$$anonymous$$ember = hitTransform.GetComponent<Group$$anonymous$$ember>();
                 Group$$anonymous$$ember myG$$anonymous$$ember = this.GetComponent<Group$$anonymous$$ember>();
                 if (G$$anonymous$$ember == null || G$$anonymous$$ember.$$anonymous$$mID == 0 || myG$$anonymous$$ember == null || myG$$anonymous$$ember.$$anonymous$$mID == 0 || G$$anonymous$$ember.$$anonymous$$mID != myG$$anonymous$$ember.$$anonymous$$mID)
                 {
                     h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, weapondamage);
                 }
             }

         }
         if (fxmanager != null)
         {
             fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
         }

     }
     else
     {
         if (fxmanager != null)
         {
             hitPoint = cam.transform.position + (cam.transform.forward * 100);
             fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
         }
     } 
 
     cooldown = firerate;
 }

 Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint)
 {

     RaycastHit[] hits = Physics.RaycastAll(ray);

     Transform closestHit = null;
     float distance = 0;
     hitPoint = Vector3.zero;

     foreach (RaycastHit hit in hits)
     {
         if (hit.transform != this.transform && (closestHit == null || hit.distance < distance))
         {
     

             closestHit = hit.transform;
             distance = hit.distance;
             hitPoint = hit.point;
         }
     }


     return closestHit;

 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast bug is making me go nnuts,Raycast bug is making me go nuts 0 Answers

How can I use RayCasting to interact with RigidBodies and Colliders? 0 Answers

Range not working c# - shooting script using raycast 0 Answers

Any way to increase raycast range? 0 Answers

Raycast don't ignore player layermask 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges