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 Misthra-Games · Aug 22, 2016 at 07:40 AM · raycastraycastingraycasthit

raycasting doesn't work

I'm using a raycast to find the tags of objects so that i can chop down trees and pick things up etc, but the ray cast only works if im at 6 feet away and that makes the game quite un-enjoyable. Please help.

Comment
Add comment · Show 3
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 aditya · Aug 22, 2016 at 07:45 AM 0
Share

Then where is your CODE ... how on this earth i will get to know whether you've set the maximum ray distance to 6 feet or not

avatar image Misthra-Games aditya · Aug 22, 2016 at 07:56 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class cQA : $$anonymous$$onoBehaviour {
     private GameObject pC;
     private GameObject player;
     private playerAction pA;
 
     // Use this for initialization
     void Start () {
         pC = GameObject.Find ("playerCamera");
         player = GameObject.Find ("FPSController");
         pA = player.GetComponent<playerAction> ();
     }
     public void checkIfHitting() {
         RaycastHit hit;
 
         Ray meleePath = new Ray (pC.transform.position, pC.transform.forward);
         Debug.DrawRay (transform.position, transform.forward* 2);
 
         if (Input.GetButtonDown ("Fire2")) {
 
             if (Physics.Raycast (meleePath, out hit, 2)) {
 
                 if (hit.collider.tag == "pTF") {
                     
                     pTF ptf = hit.collider.GetComponent<pTF> ();
                     ptf.chopTree (pA.getD$$anonymous$$G ());
                 }
                 if (hit.collider.tag == "pTHO") {
 
                     pTHO ptho = hit.collider.GetComponent<pTHO> ();
                     ptho.chopTree (pA.getD$$anonymous$$G ());
                 }
                 if (hit.collider.tag == "pTHT") {
 
                     pTHT ptht = hit.collider.GetComponent<pTHT> ();
                     ptht.chopTree (pA.getD$$anonymous$$G ());
                 }
                 if (hit.collider.tag == "pileOS") {
                     Debug.Log ("sticks");
                     pileOS pileos = hit.collider.GetComponent<pileOS> ();
                     pileos.collectSticks ();
                 }
             }
         }
             if (Input.GetButtonDown ("Fire1")) {
 
                 if (Physics.Raycast (meleePath, out hit, 3)) {
                     if (hit.collider.tag == "pileOS") {
                         Debug.Log ("sticks");
                         pileOS pileos = hit.collider.GetComponent<pileOS> ();
                         pileos.collectSticks ();
                     }
                 }
         }
     }
     // Update is called once per frame
     void Update () {
         checkIfHitting ();
     }
 }

avatar image Misthra-Games aditya · Aug 22, 2016 at 07:57 AM 0
Share

They work, but only if I'm far away

1 Reply

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

Answer by aditya · Aug 22, 2016 at 08:14 AM

try this

 Ray ray = Camera.main.ScreenPointToRay(new Vector2(0f,0f));
 
 if(Physics.RayCast(ray, out hit, 2f)){
     // Do your stuff here
 }


i m assuming that you have only one camera in scene

Comment
Add comment · Show 3 · 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 Misthra-Games · Aug 22, 2016 at 08:18 AM 0
Share

Im not cast the ray from my camera

avatar image aditya Misthra-Games · Aug 22, 2016 at 10:06 AM 0
Share

if you are not casting ray from camera then what is this variable pC ... isn't it a Camera ?

avatar image Misthra-Games aditya · Aug 22, 2016 at 11:06 PM 0
Share

Yes haha I realized that after i said it, Thanks!

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

69 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

Related Questions

Raycast is going through collider (A plane) 0 Answers

How would I project a Gameobject onto the surface of another? 0 Answers

Turning off a script when Raycast leaves object 1 Answer

Accessing Raycast collider from another script 0 Answers

Switching from object to object with Raycast Problem 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