Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DAXONATER · Aug 18, 2013 at 06:31 PM · collisionraycastobjectvector3variable

How to get Raycast to detect collision with Object?

I know that similar questions have been asked but I've searched for a long time and still can't find any solutions.

Here is the code I am working with:

     // pragma below is needed due to a UnityJS issue
     #pragma strict
     function Update(){
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (collider.Raycast (ray, hit,100.0)) {
             Debug.DrawLine (ray.origin, hit.point);
         }
     }

What I want it to do is check to see if the raycast is hitting a specified object, instead of '100.0'. I know that to do this, I need a variable that holds the vector3 data of said object, then just replace '100.0' with that variable.

The problem is that I'm not so great with Javascript. Someone please help me out? (Sorry for the lengthy post)

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
1
Best Answer

Answer by ArkaneX · Aug 18, 2013 at 08:28 PM

  1. is a length of a ray and it's not related to the object that will be hit by raycast. Calling collider.Raycast will only succeed, if you hit the game object running the above script, so you have the information which object has been hit.

If you need to use raycast which can hit different objects, then you should use Physics.Raycast, and use RaycastHit information to detect which collider was hit, using for example

 hit.collider.tag == "sometag"
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 weenchehawk · Aug 18, 2013 at 08:18 PM

From my own personal stash

 private bool IsTargetVisible()
 {
     RaycastHit ObstacleHit;
     if (Objective)    // make sure we have an objective first or we get a dirty error.
             return (Physics.Raycast(OurShip.position, Objective.position - OurShip.position, out ObstacleHit, Mathf.Infinity) &&  ObstacleHit.transform != OurShip && ObstacleHit.transform == Objective);
         else
             return false;
 } 

Sorry, it might not have been obvious, this shoots a raycast out from the forward direction of your Ship, if it collides with your Objective (which you need to set) it returns true.

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 DAXONATER · Aug 19, 2013 at 02:04 AM

Thanks for the answers, but none of them really fit what I'm trying to do. I'll be a little more specific this time :P

I'm trying to make a script that creates a raycast in front of the player, and if the ray hits the enemy, the debug log will print something out. Basically, how can I get a ray to check for a collision with an object.

EDIT: This script here seems to be a bit more managable.

 function Update() {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 100)) {
              if (hit.collider.gameObject.find("wormyguy")) {
                     Debug.Log("Success");
                 }
         }
     }

The only problem is that the ray shouldn't be coming from the mouse position (It wouldn't be accurate due to the sensitivity of the mouse) , It would be great if I could get it to come from an empty object, is there anyway to do that?

(the "wormyguy" is just a temporary name for the object) Sorry for the textbook length reply again

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 ArkaneX · Aug 19, 2013 at 08:55 AM 1
Share

Firstly, it's not an answer, and should be a comment to your original question.

Secondly, I believe you don't really know how the raycast work. In your example ray doesn't come from mouse position, but from camera position. If you want the ray to be created from some object in some direction (like in front of player), then you can use Physics.Raycast method providing ray start position and direction of the ray. Please take a look at my answer to this question.

EDIT: length of the post is not a problem. The more we know, the better we can help.

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

17 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

Related Questions

Aligning an Object to a Surface, and Rotating it 2 Answers

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

how to make a script when u collide with an object it disappears 3 Answers

Issues with Bullet Ricochet 1 Answer

Is it better to use a Raycast or a Cube gameObject for projectile collision? 2 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