- Home /
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)
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
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
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
Since the problem is solved, can you mark the question as answered, please?
Your answer
Follow this Question
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