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 cipherr · Mar 02, 2013 at 11:31 AM · enemydirectionfacingsame

Checking to see if the player is facing an enemy? (c#)

Hey guys, this is less of a "fix my error" question and more of just me seeking some advice from more seasoned programmers. I'm working on a 2.5D platformer game, and I'm making an enemy that is basically a boo from mario games (moves towards you when you look away, stops when you look at it). Generally speaking, I've got just about everything set up, except for one thing: checking to see if the player is facing the boo or not. I've been playing around with figuring out what direction the player is facing based on recent keystrokes, and that I've got working. However I can't seem to figure out how to check which direction the boo-like enemy is facing. I tried something with it's rotation, but I couldn't get that working. Does anyone have any suggestions on the best way to approach this? What I have now is just my best guess, if you know a method completely different, please go ahead and post it. Code snippets are always helpful, but not required here (however I'll post my code here if anyone wants to see). Thanks a ton

  public float                speedH = 3f;
 public float                lookingDirection;
 
 public bool                    facingHero;
 public bool                    heroFacingLeft = false;
 public bool                    heroFacingRight = true;

 void Update () {
     GameObject theHero = GameObject.FindWithTag ("Player");
     transform.LookAt(theHero.transform.position);    
     
     if(facingHero == false){
         lookingDirection = -90;
         transform.Rotate(lookingDirection, 0, 90);
        //this movement code kind of just sends the boo upwards, and needs work, 
        //but it's not what I'm asking
         rigidbody.velocity = transform.forward * speedH;
     }
     if(facingHero == true){
         rigidbody.velocity = new Vector3(0, 0, 0);
         lookingDirection = 90;        
         transform.Rotate(lookingDirection, 0, 90);
     }
     
     if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow)){
         heroFacingLeft = true;
         heroFacingRight = false;
     }
     
     if(Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow)){
         heroFacingLeft = false;
         heroFacingRight = true;
     }

Oh yeah, i should also mention that I can't alter the script for the player character (this is inevitably going to be for a group project)

Comment
Add comment · Show 4
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 cipherr · Mar 02, 2013 at 08:11 PM 0
Share

Um, I say it's a 2.5D platformer? I'm not exactly sure how I could get any more clear than that. It's just like any platformer - you move left and right through a level, you jump, etc

avatar image Fattie · Mar 02, 2013 at 08:29 PM 0
Share

lol sorry - I plain didn't see your sentence describing that :) Cheers...

normally for that you just need the four possibilities. if hero is left of boo then ... and conversely if hero is right of boo then

avatar image cipherr · Mar 03, 2013 at 12:48 AM 0
Share

Alright, I actually got it working. Just in case anyone comes across this in the future and wants to know my approach, I made bools to check which direction the enemy is facing (as suggested). The bools deter$$anonymous$$e if the enemy is facing left or right based on his last movement (as in, if his transform was last seen decreasing on increasing on the x plane). There is a small flaw where if you walk past him, it won't immediately trigger correctly, but this works well enough.

Thanks

avatar image Lockstep · Mar 03, 2013 at 02:46 AM 0
Share

Since the problem is solved, can you mark the question as answered, please?

1 Reply

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

Answer by cipherr · Mar 03, 2013 at 05:28 AM

All working now

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

12 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

Related Questions

How to snap a model to the direction being moved in, without also rotating the character controller 3 Answers

How can I respawn my RigidBodyFPSController facing new direction? 0 Answers

[Solved]Chaining projectiles 3 Answers

Help with my "CanHitTarget" function 2 Answers

How can I get my character to face the direction he is moving? 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