- Home /
2d top-down rpg direction check
Hi! I am having trouble with detecting Direction. It works just fine when I walk Up-left / Left-up or Right-down /Down-right But for Top-right/Right-top and Left-down / Down-left I have a problem - It detects wrong direction and for example my character animation is "standing -right" but if I want to shoot I shoot in UP-Direction because Direction is (0,1)
here is the code, I think that there need to be a very small fix which unfortunetly I cant figure out. Thanks for help in advance!
float absX = Mathf.Abs(speedX);
float absY = Mathf.Abs(speedY);
if ((speedX <= 0 )&&(speedY>speedX)) {
// Debug.Log("4");
Direction=new Vector2(-1,0);
}
if ((speedY >= 0)&&(speedY>speedX) && (absX<absY)) {
Direction=new Vector2(0,1);
// Debug.Log("1");
}
if ((speedX >= 0 )&&(speedY<speedX)) {
Direction=new Vector2(1,0);
// Debug.Log("2");
}
if ((speedY <= 0)&&(speedY<speedX) && (absX<=absY) ) {
// Debug.Log("3");
Direction=new Vector2(0,-1);
}
Wow you've made this so complicating.. Can you maybe give some more code? :/ For example your inputs where you move the player.
I use horizontal and vertical axis from yostick. So it's values are from -1 to 1. SpeedX - horizontal, SpeedY - vertical. I would be grateful if you could give me a simpler script because I cant figure it out.
Well show me the code and maybe I can adapt it.
Ok basically no magic happens here :
void FixedUpdate(){
speedX= CnControl.GetAxis("X") ;
speedY = CnControl.GetAxis("Y");
ctRigidbody2D.velocity= new vector2(speedX,speedY);
}
I checked speedY and speedX - the values I get from this CnControl asset - works correctly.
And in the animator I have a bland tree which plays correct animation basing on speedX and speedY, so visually it looks fine, but the Direction Vector which I need through whole game is not working properly (as described earlier)
Couldn't you just use the speedx & y values as the direction vector?
You mean my Direction=(speedX,speedY)? In that case for example when I go in Up-Right direction my speedX= 1 and speedY =1 so If I shoot a bullet, it wouldn't move in Right or Up Direction (as my animation points) but with both directions. I want to shoot in the same direction as my character stands ( I dont have 8 directions, only 4) Animator's blend tree does it very fluently ( I mean checking what direction for animation should be played)
But ok if you dont know how to help me with the code I presented then let's put it this way:
I have character which I move by joystick. I have 4 directions animation for each move so I if standing my character can be Up-Idle, Down-Idle, Left-Idle or Right-Idle Now I want to know which of four directions is it. It is simple when I move only in X or only in Y, but the problem comes when I go a little bit in X and a little bit in Y at the same time. How to check what direction is it in this case. ?
don't handle shooting with the way you move? :P
Answer by SunnyChow · Jan 07, 2016 at 04:24 AM
int face = 4;
int angle = Mathf.RoundToInt( Mathf.Atan2(speedX,speedY) /Mathf.PI/2*face)
//script below is to make the result match your format
if(angle<0)angle+=face ;
angle+=1;
I would do it with angle instead of logic check, it's more dynamic
I will check it when I return home.
So basing on speedX and speedY it will give me angle [1-4] value ?
It works almost like it should except the fact that if my character doe not move the angle is back to 1. Always when stands in any direction.
if($$anonymous$$athf.Abs(speedX)+$$anonymous$$ath.Abs(speedY)>0.1f){
//do my script
}else{
//use old value
}
to be honest our algorytthm is really good and it is working but not in the way that animator blend tree sets animation states transitions :/ . If I have speedX = 0.8, speedY = 0.9 then in that case it knows that player is in direction UP. But If I move with speedX=1 and speedY=1 then it gets confused and doesnt set direction corectly so in the end I get cases when moving with full speed(=1) where animation played is StandingRight and Direction is Up.
If you haev another suggestion it would be good, but I will try to give you an exampe project and you will see in debug.log the Direction and you will also be able to see what I really mean with the animator
Hi! I managed to have some time and I made a test scene where you can see exactly what the problem is. In debug.log you can see the "angle" and the Direction vector which I need.
Try to move character by joystick, if you don;t move with full speed and you will change the speed in the way to see how animation transitions works you will see that they work just fine and comatibly with your suggested algorithm to detect Direction. Howeever if you move with full speed and try to change direction for example from right to up and up to right you will see that animator does his work well, but the direction in debug.log is not proper.
Please check this out, I give an upload here.
Refreshing... Please, I really hope you can help me basing on this test scene I uploaded
Hi, I reuploaded my test scene where you can see the problem even after using your solution.