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 SPOTNINJADUD7890 · Oct 21, 2016 at 03:23 AM · c#unity 5raycastphotonmultiplayer-networking

How to limit this particular raycast distance?

I need to make the rays distance 2 if the playerpref Wep = 1. I have tried but i have failed alot please help me!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerShootingAss : MonoBehaviour {
 
     public float fireRate = 0.5f;
     public float cooldown = 0;
     public float cooldown2 = 0;
     public float damage = 25;
     FXManager fxManager;
     private int Wep;
     public bool cooldownChange = false;
     public bool cooldownChange2 = false;
     public Image reloaddefault;
     public Image reloadsniper;
     void Start(){
         fxManager = GameObject.FindObjectOfType<FXManager>();
 
         if (fxManager == null) {
             Debug.LogError ("couldn't find an fx manager");
         }
         reloaddefault = transform.FindChild("FirstPersonCharacter").FindChild ("Laserbow").FindChild ("PlayerCanvas").FindChild ("HealthBGgun1").FindChild ("Health").GetComponent<Image> ();
         reloadsniper = transform.FindChild("FirstPersonCharacter").FindChild ("Sword").FindChild ("PlayerCanvas").FindChild ("HealthBGgun2").FindChild ("Health").GetComponent<Image> ();
     }
 
 
 
     void Update () {
         Wep = PlayerPrefs.GetInt ("Wep");
         cooldown -= Time.deltaTime;
         cooldown2 -= Time.deltaTime;
         reloadsniper.fillAmount = (float)cooldown2 / (float)fireRate;
         reloaddefault.fillAmount = (float)cooldown / (float)fireRate;
 
         if (Input.GetButton ("Fire2")) {
             transform.FindChild ("FirstPersonCharacter").FindChild ("PlayerCanvas").FindChild ("HealthBG").gameObject.SetActive(false);
             transform.FindChild ("FirstPersonCharacter").FindChild ("crosshair").gameObject.SetActive (false);
         } else { 
             transform.FindChild ("FirstPersonCharacter").FindChild ("PlayerCanvas").FindChild ("HealthBG").gameObject.SetActive(true);
             transform.FindChild ("FirstPersonCharacter").FindChild ("crosshair").gameObject.SetActive (true);
         }
 
         if (Input.GetButton ("Fire1")) {
             Fire ();
         }
         if (Wep == 0) {
             fireRate = 3f;
             damage = 100;
         } else {
             fireRate = 1f;
             damage = 50;
         }
         if (Input.GetButtonDown("Wep1")) {
             cooldownChange = true;
         }
         if (Input.GetButtonDown("Wep2")) {
             cooldownChange2 = true;
         }
         if (cooldownChange == true){
             cooldownChange = false;
         }
         if (cooldownChange2 == true){
             cooldownChange2 = false;
         }
     }
 
     void Fire(){
         if (Wep == 0) {
             if (cooldown > 0) {
                 return;
             }
         } else {
             if (cooldown2 > 0) {
                 return;
             }
         }
 
 
 
         Debug.Log ("We shot");
 
             Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
 
         Transform hitTransform;
         Vector3 hitPoints;
 
         hitTransform = FindClosestHitObject (ray, out hitPoints);
 
         if (hitTransform != null) {
             Debug.Log ("We hit: " + hitTransform.name);
             //hitInfo.point
 
             Health h = hitTransform.GetComponent<Health> ();
 
             while (h == null && hitTransform.parent) {
                 hitTransform = hitTransform.parent;
                 h = hitTransform.GetComponent<Health> ();
 
             }
 
             if (h != null) {
                 h.GetComponent<PhotonView> ().RPC ("TakeDamage", PhotonTargets.All, damage);
             }
 
             if (fxManager != null) {
                 DoGunFX (hitPoints);
 
             }
         } else {
             if (fxManager != null) {
                 DoGunFX (hitPoints);
                 WeaponData wd = gameObject.GetComponentInChildren<WeaponData> ();
                 hitPoints = wd.transform.position + (wd.transform.forward*100);
 
             }
         }
 
         if (Wep == 0) {
             cooldown = fireRate;
         } else {
             cooldown2 = fireRate;
         }
     }
 
     void DoGunFX(Vector3 hitPoints){
         WeaponData wd = gameObject.GetComponentInChildren<WeaponData> ();
         fxManager.GetComponent<PhotonView> ().RPC ("SniperBulletFX", PhotonTargets.All, wd.transform.position, hitPoints);
     }
 
     Transform FindClosestHitObject(Ray ray, out Vector3 hitPoints) {
 
         RaycastHit[] hits = Physics.RaycastAll (ray);
 
         Transform closestHit = null;
         float distance = 0;
         hitPoints = Vector3.zero;
 
         foreach (RaycastHit hit in hits) {
             if (hit.transform != this.transform && (closestHit == null || hit.distance < distance)) {
 
                 closestHit = hit.transform;
                 distance = hit.distance;
                 hitPoints = hit.point;
             }
         }
 
         return closestHit;
 
     }
 }
 


Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SPOTNINJADUD7890 · Oct 22, 2016 at 02:35 PM

Fixed final code

RaycastHit[] hits = Physics.RaycastAll (ray); if (Wep == 0) { hits = Physics.RaycastAll (ray); } else { hits = Physics.RaycastAll (ray, 2f); }

Comment
Add comment · Share
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
0

Answer by b1gry4n · Oct 21, 2016 at 03:41 AM

It looks like you are getting your ray using the camera transform and that transforms "forward". The value returned will be a Vector3 where each value is between 0 and 1. Drawing that ray, it will always be 1 unit long. To make it 2 units long, multiply it by 2.0

      if (Wep == 1) {
           Vector3 val = Camera.main.transform.forward*2.0f
           ray = new Ray (Camera.main.transform.position,val );
         } 
Comment
Add comment · Show 5 · Share
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 SPOTNINJADUD7890 · Oct 21, 2016 at 09:12 PM 0
Share

this does not work giving me the error Assets/scripts/PlayerShootingAss.cs(86,25): error CS0019: Operator *=' cannot be applied to operands of type UnityEngine.Ray' and `float'

avatar image SPOTNINJADUD7890 · Oct 21, 2016 at 09:14 PM 0
Share

also how would i change the value back to the infinite range when i switch back?

avatar image SPOTNINJADUD7890 · Oct 21, 2016 at 10:18 PM 0
Share

seems like it isnt working it is still shooting the full distance...

avatar image b1gry4n SPOTNINJADUD7890 · Oct 21, 2016 at 10:23 PM 0
Share

You want the object shot to be limited in distance or you want the ray to cast a specific distance? The question says

How to limit this particular raycast distance?

if you want a raycast to shoot a specific distance I suggest you rework your code.

 if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2.0f, mask){
 //hit something
 Debug.Log(hit.transform.gameObject.name);
 }

where "2.0f" would be the length of the cast ray.

avatar image SPOTNINJADUD7890 b1gry4n · Oct 21, 2016 at 10:42 PM 0
Share

I dont know were to put this. lol please tell me

avatar image
0

Answer by Bunny83 · Oct 21, 2016 at 11:02 PM

The length of the Ray direction is not relevant. The Physics.RaycastAll method has an optional parameter where you can specify the max distance. If you don't pass any distance (like you did) the ray will have infinite length.

Comment
Add comment · Show 2 · Share
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 SPOTNINJADUD7890 · Oct 21, 2016 at 11:13 PM 0
Share

i still dont understand were i would put this in my code i cannot find raycast all.

avatar image SPOTNINJADUD7890 · Oct 22, 2016 at 02:23 PM 0
Share

could you tell me were to put this i cannot find Physics.RaycastAll in my code

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

254 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Photon Networking - How to move all players from game scene to current room scene? 0 Answers

Unity 5 - My raycast is hitting my player, and I can't figure out why 2 Answers

UNET Clients Laggy 0 Answers

Both players reach same time how to get result draw in photon unity 1 Answer

Raycast over Photon 0 Answers


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