- Home /
How to wall-walk?
I have tried to ask several times but I still didn't mange to make it work. So I want to make my player walk on walls just like it would on the ground. Therefore I need to change the gravity and rotate the camera, that worked fine. But then I also need the player to rotate based on the hit.normal of the ground and move him based on the input from the joystick and make that align with the surface (or perpendicular to the hit.normal) see image. This is where I can stuck every time. I would really appreciate it if u could correct my code so that it would work. I will give 20 points away if it worked :).
1.I'am using a Rigidbody
2. I'am using a joystick that returns a vector form -1 to 1 like (3, 0f, -8).
private void Update()
{
h = Joystick.Horizontal;
v = Joystick.Vertical;
movement = (transform.forward * v) + (transform.right * h);
//or movement.Set(h, 0f, v)
Debug.Log(movement);
}
private void FixedUpdate()
{
Physics.Raycast(transform.position, -(transform.up), out hit, RayLengh)
transform.up = hit.normal;
transform.rotation = Quaternion.LookRotation(movement, transform.up);
playerRig.MovePosition(transform.position + movement);
}
Hmm, have you thought about rotating the world ins$$anonymous$$d?? Then you don’t have to change gravity and movement is the exact same. You could make an empty object a parent of the world objects, including the lights to keep them consistent with the shadows, and just rotate that empty.. There might be reasons why this wouldn’t work though, like other characters or objects that are affected by rigidbodies, etc.
Yeah I have thought about that I was only worried that it would cost a lot of performance is that true?
Answer by laurG · Mar 27, 2019 at 06:02 AM
Afaik, is not a good idea to ever set the Up of a transform.
I wrote this code just now, but did not test. Add me on Discord: Laur#6047 . I will get it done in the afternoon and help you.
public Vector3 normal = Vector3.up;
private void FixedUpdate()
{
if (Physics.Raycast(transform.position, -(transform.up), out hit, 100))
{
transform.position = hit.point;
normal = hit.normal;
}
}
private void Update()
{
transform.rotation = Quaternion.LookRotation(movement.normalized, normal);
}
This code did unfortunately not work for me as it would make my player spin or move 'glitchy'. @laurG
Answer by Patico · Mar 27, 2019 at 09:22 AM
You can use Custom Gravitations Kit for that, and walk everywhere, even on ceiling =) https://assetstore.unity.com/packages/tools/physics/custom-gravitations-kit-17789
Thank you, but I will first try to understand the physics behind it myself.
Your answer
![](https://koobas.hobune.stream/wayback/20220612194331im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
smoothly rotate my rigidbody with CapsuleCollider to facing the fall direction 0 Answers
Rigidbody along with up-vector 2 Answers
Rigidbody spins out of control when colliding into corner 0 Answers
Object slope rotation issue 0 Answers
Is there any way to keep increasing an object's angular velocity? 1 Answer