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 Kiara Astra · Apr 19, 2016 at 06:56 PM · c#raycastfirst-person-controller

Raycast is not hitting the first person controller, but works fine with third person character controller

I'm making a game that lets you swap between first and third person, and in it the enemy is made to check line of sight using a raycast (in the same way as the stealth tutorial enemies) to see if there is anything in the way of seeing the player. This all works fine in third person when there is an actual model for the player, but when in first person and just using the first person controller the raycast does not register as hitting it. I've tried looking with the debug drawray but it's not given me much more information. When I've tried looking it up people have usually had the opposite problem!

The playerInSight variable does not get set to true when I'm in first person.

here is the method that includes the raycast:

 void OnTriggerStay (Collider other)
 {
     // If the player has entered the trigger sphere...
     if(other.gameObject.CompareTag("Player"))
     {
         playerDistance = Vector3.Distance(other.transform.position + transform.up, transform.position + transform.up);
         // By default the player is not in sight.
         playerInSight = false;
         
         // Create a vector from the enemy to the player and store the angle between it and forward.
         Vector3 direction = other.transform.position - transform.position;
         float angle = Vector3.Angle(direction, transform.forward);
         
         // If the angle between forward and where the player is, is less than half the angle of view...
         if(angle < fieldOfViewAngle * 0.5f)
         {
             RaycastHit hit;
             
             // ... and if a raycast towards the player hits something...
             Debug.DrawRay(transform.position + transform.up, direction.normalized*30, Color.red, 0.1f, true);
             if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius*20))
             {
                 // ... and if the raycast hits the player...
                 if(hit.collider.gameObject.CompareTag("Player"))
                 {
                     // ... the player is in sight.
                     playerInSight = true;
                     
                     // Set the last global sighting is the players current position.
                     lastSighting = other.transform.position;
                 }
             }
         }
     }
 }
Comment
Add comment · Show 9
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 Cherno · Apr 19, 2016 at 09:09 PM 0
Share

Why is the Debug Ray's direction multiplied by 30?

avatar image Kiara Astra Cherno · Apr 19, 2016 at 10:16 PM 0
Share

because it's a distance not a direction in the debug one

avatar image miahmiah · Apr 19, 2016 at 10:17 PM 0
Share

Does this include the code for switching between first and third person? It's hard to tell what's going on without. $$anonymous$$ake sure the first person controller setup has a capsule collider attached to the first person game object?

avatar image Kiara Astra miahmiah · Apr 19, 2016 at 10:54 PM 0
Share

No it doesn't, but it doesn't really make a difference because the game starts in first person and it doesn't work then, so I don't think the change is breaking it. The first person controller is unity's standard asset FPSController, it comes with a character controller on it and from what I've looked up that -is- a collider, so it should work. The character controller is the only collider on the third person one as well and that works fine.

The only thing I can possibly think is that it doesn't like it because it's a transparent object... but I can't find anything in the documentation that says that should be a problem since it's based on colliders...

avatar image Cherno · Apr 19, 2016 at 11:00 PM 0
Share

Are both the third person model collider and the CharacterController in the same layer? Are both tagged as "Player"?

avatar image Kiara Astra Cherno · Apr 19, 2016 at 11:40 PM 0
Share

yup. They are not both active at the same time though

avatar image Cherno Kiara Astra · Apr 20, 2016 at 01:14 AM 1
Share

I assume this issue wasn't corrected in the last three years?

Raycast Doesn't Hit Character Controller

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Clicking on an Object to Make it the Variable Target 1 Answer

Raycasting on mask doesnt work 2 Answers

Help with "inventory" system c# 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