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 infinitypbr · Jun 21, 2013 at 09:59 PM · raycastraycastinghit

Raycasting doesn't seem to "hit" a Character Controller? [image]

I have code to check to see if an enemy can "see" the player. Unfortunately it seems that the Raycast isn't hitting the player, who has a Character Controller attached.

I've googled, but none of the other answers seem to answer this. Any idea why Raycast wouldn't hit the player?

[the yellow line is the Line drawn before the raycast, the cyan line is the line drawn between the enemy and the "hit" object. If the palyer was being hit, you'd see it in the consol and a lot of cyan lines, equal to the yellow lines.]

 function OnTriggerStay (other : Collider)
 {
     if (other.gameObject.tag == "Player")
     {
         var rayDirection = other.gameObject.transform.position - transform.position;
         rayDirection.y += 1;
         var currentPosition = transform.position;
         currentPosition.y += 1;
         var hit : RaycastHit;
         Debug.DrawLine (currentPosition, other.gameObject.transform.position, Color.yellow, 3);
         if(Physics.Raycast(currentPosition, rayDirection, hit, viewDistance)) 
         {
             Debug.DrawLine (currentPosition, hit.collider.gameObject.transform.position, Color.cyan, 3);
             print ("Hit: " + hit.collider.gameObject.transform.name);
             if(hit.collider.gameObject.tag == "Player")
             {
                 print("Player found");
             }
         }
         
     }
 }

alt text

screen-shot-2013-06-21-at-2.54.jpg (137.6 kB)
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

2 Replies

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

Answer by Owen-Reynolds · Jun 21, 2013 at 11:29 PM

Raycasting is tricky, and you should have a feel for Vector3 math and offsets vs positions. For example, adding +1 to rayDirection.y changes the angle by a semi-random amount (which is usually not what you want.)

I'd say rewrite lines 5-8, by figuring out where you want to shoot from, and where you want to shoot to. Then the last line computes the ray using computedTarget-ComputedStart.

Also, Debug.DrawRay doesn't use the same math as Raycast. Close, but not the same. So you can have where drawRay seems to hit/miss, but the raycast is different.

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 infinitypbr · Jun 21, 2013 at 11:47 PM 0
Share

That was it -- thanks. Here is the working code:

 function OnTriggerStay (other : Collider)
 {
     if (other.gameObject.tag == "Player")
     {
         var shootTo = Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z);
         var shootFrom = Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
         var rayDirection = shootTo - shootFrom;
         var hit : RaycastHit;
         Debug.DrawLine (shootFrom, rayDirection, Color.yellow, 3);
         if(Physics.Raycast(shootFrom, rayDirection, hit, viewDistance)) 
         {
             Debug.DrawLine (shootFrom, hit.collider.gameObject.transform.position, Color.cyan, 3);
             print ("Hit: " + hit.collider.gameObject.transform.name);
             if(hit.collider.gameObject.tag == "Player")
             {
                 print("Player found");
             }
         }
     }
 }


  • shootTo and shootFrom can be simplified.

avatar image Owen-Reynolds · Jun 22, 2013 at 04:42 PM 0
Share

The way you were originally adding math is also fine: shootFrom=transform.position then shootFrom.y+=1;

avatar image
0

Answer by Slobdell · Jun 21, 2013 at 10:39 PM

Dude this is at least the 2nd time today you've posted the same question, thought I saw a third. Please don't repeat. Again, I will ask you what is that calculation for raydirection doing? Why do you think that would point to your object? This is copy and pasted straight from the docs.

 public class example : MonoBehaviour {
     void Update() {
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position, fwd, 10)){
             print("There is something in front of the object!");
         }
     }
 }
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 Slobdell · Jun 21, 2013 at 10:40 PM 0
Share

And since I know you will ask, change Vector3.forward to your object you want to shoot the ray at

avatar image infinitypbr · Jun 21, 2013 at 11:37 PM 0
Share

Two questions. The first turned out to be a question of Debug.DrawLine (the Yellow line).

This question is about the raycast (Cyan line) not hitting the player. The cube is hit when the player is behind the cube.

The calculation should give the direction required to point at the player. The image below is when I use transform.TransformDirection(Vector3.forward) -- as expected, the ray only shoots forward of the enemy, so only hits the player when the player is in DIRECT line of site. I'm attempting to find out if the player is in the field of view, not line of site.

alt text

And this is what happens when I use hit.collider.gameObject.transform.position -- the blue line (raycast) shoots behind the enemy, not towards the player.alt text

screen shot 2013-06-21 at 4.39.03 pm.png (479.9 kB)
avatar image Slobdell · Jun 21, 2013 at 11:44 PM 0
Share

Did you not see my comment that says change vector3.forward to the position of your object you're trying to hit?!?!?!?!

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

16 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

Related Questions

Help destroying gameObject on RayCastHit? 1 Answer

Getting a point on ray 1 Answer

Change raycasting 1 Answer

How to use raycast on generated objects. 0 Answers

Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 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