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 /
This question was closed Feb 08, 2019 at 09:01 PM by nilserlingh for the following reason:

Figured out the problem

avatar image
0
Question by nilserlingh · Feb 07, 2019 at 10:37 AM · aienemyenemy airaycasthit2d

Enemy walking off platform in 2D mode

In my game the enemy is suposed to be walking back and forth on top of a platform. I use raycast to notice the edge, but it only works once. The enemy will start on the left side, before moving to the right side and turning. The problem is that the enemy does not turn when it hits the left side. The ray is cast from an empty object right in front of the enemy. The object does seem to follow the enemy for the entire duration, but still it wont work. Anyone got a clue of what the problem might be?

Script for the rays:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Patrol : MonoBehaviour
 {
     public float speed;
     public float distance;
 
     private bool movingRight = true;
     public Transform groundDetection;
 
     void Update()
     {
         transform.Translate(Vector2.right * speed * Time.deltaTime);
 
         RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
 
         if (groundInfo.collider == false)
         {
             if(movingRight == true)
             {
                 transform.eulerAngles = new Vector2(0, 180);
                 movingRight = false;
             }
             else
             {
                 transform.eulerAngles = new Vector2(0, -180);
                 movingRight = true;
             }
         }
     }
 }
 
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

1 Reply

  • Sort: 
avatar image
0

Answer by Chimer0s · Feb 08, 2019 at 08:13 AM

Is the ground detection gameobject a child of the enemy? You say in your description that the object does seem to "follow" the enemy for the entire duration. The obvious question to me, without being able to see it running, is whether the object is on the correct side when returning. If it's always to the right of the object it would detect a lack of floor when going right, but wouldn't be able to do so until the enemy has already stepped off when going left. That may seem simple but the code doesn't seem to have any glaring issues to me, so I have to assume it's something to do with the object the raycast is coming from.

Comment
Add comment · Show 9 · 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 nilserlingh · Feb 08, 2019 at 09:40 AM 0
Share

The groundDetection object is a child of the enemy object yes, and it flips at the same time as the rest of the sprite. Yet when it walks towards the left it just walks right into the air. Good thing if there is no errors in the code still!

avatar image Chimer0s nilserlingh · Feb 08, 2019 at 10:17 AM 0
Share

Have you tried doing a debug.drawray() to make sure there isn't something weird happening and the ray isn't going in the wrong direction or hitting something unexpected? Also, is there any chance that there's an unexpected trigger collider off the left side of the platform?

avatar image nilserlingh Chimer0s · Feb 08, 2019 at 10:54 AM 0
Share

alt text

The ray seem to be at the correct place, and going in the correct direction. There is nothing anywhere beneath the left side of the platform either. Still helpfull being able to see the ray though, even if it dident solve anything. Thats the object after it has moved of the platform to the left.

Show more comments

Follow this Question

Answers Answers and Comments

167 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 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 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 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 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 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 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 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 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 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

How would I animate a 2D enemy with AI made with A*? 0 Answers

Enemy AI Patrol Points Array Index Out of Range? 2 Answers

enemy raycast to detect player 1 Answer

Kill character on impact 1 Answer

Why do the AI get caught on trees? 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