Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ahmad_Talal · Jan 17, 2014 at 03:38 PM · guiraycasting

Need help with Raycast

Hello guys. I have this script which works fine with the first script but I want the second script to be working 1st script;

 public var CapsuleObject : GameObject; 
 private var CalButton = false;
 
 function Update () {
 
     CapsuleObject = GameObject.Find("Capsule");
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      
     if (Physics.Raycast(ray, 50))
     {
     CalButton = true;
     }
 
 }
 
 function OnGUI () {
 
     if(CalButton == true)
     {
     if(GUI.Button(Rect(100,100,100,50),"VerticalDistance"))
      
     {
     guiText.text = "" + CapsuleObject.transform.position.y;
     }
     }
     }





and second script is;

     public var CapsuleObject : GameObject;
     private var CalButton = false;
      
      
     function Update () {
      
     var hit : RaycastHit;
     CapsuleObject = GameObject.Find("Capsule");
      
     if (Physics.Raycast(transform.position,transform.forward,hit,150))
     {
     CalButton = true;
     }
      
     }
      
      
      
      
     function OnGUI ()
     {
     if(CalButton == true)
     {
     if(GUI.Button(Rect(100,100,100,50),"VerticalDistance"))
      
      
     {
     guiText.text = "" + CapsuleObject.transform.position.y;
     }
     }
     }



The first one works well but the second one doesn't and I want the second one to be working. please help

Comment
Add comment · Show 5
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 robertbu · Jan 17, 2014 at 04:40 PM 0
Share

You need to describe what you are trying to do and how it is not working. The difference between the two is that the first one is raycasting from the mouse position, and the second is raycasting from the position of the game object this script is attached to in the forward direction of that object. You might want to add this at line 9 to visualize the raycast:

 Debug.DrawRay(transform.position, transform.forward * 150.0);

There are a couple of other issues with your code. Not sure if this is the way you want it, but once 'CalButton' gets set to true, it is true always regardless of the movement of this object. And second you are calling 'GameObject.Find()' in Update. $$anonymous$$ove this line into Start().

avatar image Ahmad_Talal · Jan 17, 2014 at 07:28 PM 0
Share

Thnx Robert for commenting. What I want that When I click on the "CalButton" then it the object needs to shoot another object, but the another object must be in rang. means that I have to aims on the object which is in the rang of Raycast. means that the another object must be touched with Raycast ray. means that my shooter will only shoot when the another object is in touch with Raycast Ray. thnx

avatar image robertbu · Jan 17, 2014 at 07:37 PM 0
Share

I'm still confused about what you want to happen. Let me assume the Capsule is the target. And let me assume that enabling the CalButton is considered a hit. Do you care if the object is in front of this object? That is must the positive 'z' side of the object this script is attached to be facing the object in question for a hit to occur? If not, you don't need a Raycast. You could just do:

 if (Vector3.Distance(transform.position, GameObject.Find("Capsule").transform.position) < 150) {
     CalButton = true;
 }

Again, the Debug.DrawRay() will tell you where you are casting your ray.

avatar image Ahmad_Talal · Jan 18, 2014 at 10:37 AM 0
Share

I did what you told me, it worked but what I figured out that the Debug.DrawRay() is not rotating with the camera rotation. I applied the script to camera. It produce the ray well, I how I am gonna rotate it. I also checked the unity documentation, which helps me a lot but I didn't find the solution. thnx for the help,

avatar image Ahmad_Talal · Jan 18, 2014 at 10:42 AM 0
Share

$$anonymous$$y script is right, but that rotating thingy is making it not workable. I simple just want that the Debug.DrawRay() rotates with the applied camera. $$anonymous$$y question is clear now. What I want that when the DrawRay hit the capsule it will activate the button. All my codes are ready to go except that rotating thingy... thnx for the help

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Make a crosshair turn red when over an enemy 1 Answer

how to "blink" more often? 2 Answers

Erroe when resizing GUITextures in C# file 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