• 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 /
  • Help Room /
avatar image
0
Question by ollie_payne · Jun 05, 2016 at 04:11 PM · raycastraycasthit2d

Raycast2D not detecting a certain object

I have made a basic enemy ai script for my 2D game. I have the enemy raycasting in the direction he is facing, to detect for the player. However, the enemy detects all other objects but never seems to detect the player. I am completely stumped so any and all help is appreciated. Thank you.

 using UnityEngine;
 using System.Collections;
 
 public class SkeletonSoul : MonoBehaviour {
 
     RaycastHit2D hit;
 
     Vector2 rayDirection;
 
     bool movingLeft = false;
     bool movingRight = true;
 
     public Transform destinationLeft;
     public Transform destinationRight;
 
     public float patrolSpeed;
     public float attackChargeSpeed;
 
     void Update () {
         hit = Physics2D.Raycast (transform.position, rayDirection, 1f);
         Debug.DrawRay (transform.position, rayDirection, Color.red);
 
         if (hit.collider.name != null) {
             Debug.Log (hit.collider.name);
         }
 
         //The skeleton soul is moving to the right
         if (movingRight == true) {
             rayDirection = Vector2.right;
 
             transform.localScale = new Vector3 (1, 1, 1); //Make the skeleton soul face to the right when moving right
             transform.position += Vector3.right * patrolSpeed * Time.deltaTime;
 
             if (transform.position.x > destinationRight.position.x) {
                 movingLeft = true;
                 movingRight = false;
             }
         }
 
         //The skeleton soul is moving to the left
         if (movingLeft == true) {
             rayDirection = Vector2.left;
 
             transform.localScale = new Vector3 (-1, 1, 1); //Make the skeleton soul face to the left when moving left
             transform.position += Vector3.left * patrolSpeed * Time.deltaTime;
 
             if (transform.position.x < destinationLeft.position.x) {
                 movingRight = true;
                 movingLeft = false;
             }
         }
     }
 }
 

Comment
Add comment · Show 2
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 Hellium · Jun 05, 2016 at 03:56 PM 0
Share

Are you sure the player has a collider ?

avatar image ollie_payne · Jun 05, 2016 at 08:15 PM 0
Share

@Hellium The player has a Box Collider 2D attached to it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kristof1104 · Jun 05, 2016 at 08:23 PM

Got this from the wiki:

"If you move Colliders from scripting or by animation, you need to allow at least one FixedUpdate to be executed so that the physics library can update before a Raycast will hit the Collider at its new position".

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Using raycast to detect line drawn with mouse 0 Answers

Is there any way how to do raycasts like this? 0 Answers

Strange Raycast problem 0 Answers

Detect when Raycast DOESN'T hit layer. 1 Answer

2D How to reflect a raycast with a line renderer 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