- Home /
Logic Required
Hi I am making a game in 2d where cahracter can walk on any type of platform that comes in front of him. what I am doing is casting a ray from the character and if it hit the wall I am giving the wall rotation to the character but this logic fails if the wall formed is below the base .You can see the attached image to be clear
please give any idea if you have
As I understood (correct me if I am wrong) you are trying to make wall-walking game, like Spider-$$anonymous$$an? In this case, maybe, one of many possible solutionsm is to rotate not the wall, but the character.
converted my answer to the comment, so you can get more views.
Can you give any idea so that I can implement this to code . just if you have to make such game how would you approach it .
Answer by fafase · Feb 16, 2015 at 10:12 AM
Here something to start with.
Apply a constant gravity in the current down direction. This way if you are always heading in your feet direction.
Have two raycasts at your feet casting front and back at a short distance but long enough to hit the walls.
The previous raycasts are used only if you have no collision with your feet.
Use the normal of the hit to rotate your character
Now the consequences of that: your guy is standing, it is going down and collides with the ground, nothing special. As you reach the wall to the right, nothing happens. If you jump, the raycast is called, it hits the wall if you are close enough (else nothing happens and you are going down). If it hits the wall, you get the hit.normal and rotate your guy accordingly. At this point you are horinzontal, and gravity is to the right and the jump should have got you far enough from the ground so that the casts are not hitting the ground. You will then naturlly be stuck to the wall by the fake gravity.
You will have to try and tweak until it suits your needs but that should get you started at least.
Great answer As I am also trying somthing like that I am just messed up with gravity part I m not getting how to update the gravity as there can be four condition (0,-9.8),(-9.8,0),(0,9.8),(9.8,0) there will also be other cases like (9.8,9.8) and (-9.8,-9.8) etc Do you have any logic for this or algorithm
To rotate the character I am using
this.transform.eulerAngles = hit1.collider.transform.eulerAngles;
this.transform.parent= hit1.collider.transform;
To give constant velocity in direction of heading
transform.localPosition = transform.localPosition + new Vector3 (.04f,0,0);
Can you tell me how to get the constant gravity in current down direction . And Do i have to take hit.normal or this will work as its rotating the characrter
you don't need to update the gravity if you use the transform.down or -transform.up.
Well, it depends on you process the rotation. If you snap it then right after you can use transform.down. If you lerp it then you might have to wait for the rotation to be done to change the gravity value. All in all, I would not rely on those 4 values you have, just the transform of your object.
Note that transform.up/down are relative to the current rotation which is why you do not need to care about the values you defined.
Thannxxxx fafase it worked . what I did was AddForceRelative . you a developer with very clear logic , Though there are some glitches I will solve it ... Thank once again
Your answer
![](https://koobas.hobune.stream/wayback/20220613183430im_/https://answers.unity.com/themes/thub/images/avi.jpg)