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 /
  • 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.

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

RayCastHit 2d with layerMask not workin 0 Answers

How can i calculate where my balls gonna crash ( i mean which obj.) 0 Answers

Raycast2d detects hit when theres not 1 Answer

Tag doesn't work when gameObject is a child of the Main camera? 1 Answer

Is it possible to add Angle to Raycast2D? 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