Face Character Towards Mouse

Ask gaming related questions
lp230889
Posts: 16
Joined: 06 May 2016, 05:49

Face Character Towards Mouse

14 Dec 2017, 06:45

Hi

I need a little help with this part of my code, it works but not as well as i'd like.
It seems to get confused which way to face the close to the center the mouse gets.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Example:
Attachments
example.png
example.png (3.69 KiB) Viewed 136 times
wolf_II
Posts: 1921
Joined: 08 Feb 2015, 20:55

Re: Face Character Towards Mouse

14 Dec 2017, 08:17

I can't test your code. But I can give a tip: (might not help though)

I would make the code more readable to humans. The interpreter will not struggle because of it, it is fine either way.
The maintainer of the code will struggle more to understand one way than the other way.

E.g.: I changed the longest line to something like this:

Code: [Select all] [Download] GeSHi © Codebox Plus

		if ( nowY < charCenterY - deadZone    )
&& ( nowX > charCenterX - workingArea )
&& ( nowX < charCenterX + workingArea )
{
; blah blah
}
I then noticed your code does not handle all cases.
Maybe if you handle all cases it would be fixed? I have no idea. Your code is not testable for me.
lp230889
Posts: 16
Joined: 06 May 2016, 05:49

Re: Face Character Towards Mouse

14 Dec 2017, 09:03

Hi thanks for advice, i'll keep it in mind.

I fixed the code on my own using angles instead, works great now

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
evilC
Posts: 4181
Joined: 27 Feb 2014, 12:30

Re: Face Character Towards Mouse

15 Dec 2017, 09:44

When working with angles like this, esp if you want it to wrap around, you can get sneaky.

Round(angle / 90) will give you a "Cardinal" (Pure North, South, East or West). It still wraps around, but it is a simple check you need to make anyway.
Also, it appears as if the game is isometric display or something, but in that case, just shift the angle around before you make your wrap-around adjustments, and then you will be dealing with cardinals, again making it easier.

for example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Also, note that if you can reduce the angle down to integers, you can then also avoid if/else blocks by using an array of functions:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
evilC
Posts: 4181
Joined: 27 Feb 2014, 12:30

Re: Face Character Towards Mouse

15 Dec 2017, 09:54

Or, if all your directions do basically the same thing, but with parameters, you could do like this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 5 guests