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 /
avatar image
0
Question by twoface262 · Jun 29, 2011 at 02:21 AM · textureraycastenemycrosshair

Raycast questions

I have a few questions about raycasting I'm using a raycast script for my gun heres the code.

     Screen.lockCursor = true;
     function Update () {
 // Put this in your update function
     if (Input.GetButtonDown("Fire1")) {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 100)) {
             print ("Hit something");
         }
     }
 }


I have a few questions though. 1. How would I use a texture as a crosshair in the center of the screen? 2. How would I make the enemys react to the raycast hitting them would it be something like "OnRaycastEnter()"? 3. How would I make the raycast go in the direction of the crosshair texture instead of the mouse?

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 testure · Jun 29, 2011 at 02:51 AM 1
Share

formatted your code for you- make sure when you post a question that your code is readable.

1 Reply

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

Answer by testure · Jun 29, 2011 at 02:55 AM

1) A texture as a crosshair is just a GUITexture in the middle of your screen. simple as that. Here's how you use it:

http://unity3d.com/support/documentation/Components/class-GuiTexture.html

2) The answer is in the code you posted. if your raycast hits something, it will drop into the conditional statement that says print("Hit something"). from there you can access the object that it hit using hit.collider (what you do with it after that is up to you). Here's how you use raycasting:

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

3) use Camera.ViewportPointToRay to create your ray:

http://unity3d.com/support/documentation/ScriptReference/Camera.ViewportPointToRay.html

You'll notice that I linked the Unity docs in every answer. All of your questions were solved by one source of information. Please read the unity docs, you will learn a ton from it.

Comment
Add comment · Show 1 · 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 twoface262 · Jun 29, 2011 at 03:04 AM 0
Share

Thank you those where very helpful.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change crosshair text when hitting a trigger 0 Answers

Smart Crosshair 4 Answers

How to make a crosshair move with the player 1 Answer

Raycast based on crosshair on screen 1 Answer

How to detect an object that comes in the "Crosshair" field of view? 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