- Home /
Programming Wall Climbing
Basically, lets say the surface of the wall is "latchable" (which would really be a box collider in my case adjusted to the side of the wall.) I already had assistance with latching onto a ledge and being able to move left and right on it. http://answers.unity3d.com/questions/18430/ledge-hanging-help-box-triggers-are-being-used
I figured in theory that if I copied the code and tweaked it to allow the player to move up and down it would work out. For some reason, it ignores the code that lets me go left and right and now I can only go up and down.
Here is a snippet:
// Wall Movement controls if (onWall && wallTransform) { // Lock camera for short period when transitioning moving & standing still lockCameraTimer += Time.deltaTime; if (isMoving != wasMoving) lockCameraTimer = 0.0;
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
//If the camera is behind/beside, right is right. Otherwise, right is left.
if(Vector3.Dot(Camera.main.transform.forward, transform.forward) < 0)
moveDirection = h * wallTransform.right;
else
moveDirection = h * -wallTransform.right;
//If the camera is behind/beside, down is down. Otherwise, down is up.
if(Vector3.Dot(Camera.main.transform.forward, transform.forward) < 0)
moveDirection =v * -wallTransform.up;
else
moveDirection = v * wallTransform.up;
}
I don't think Super $$anonymous$$ario Galaxy has anything to do with it.
Answer by skovacs1 · Aug 23, 2010 at 02:32 PM
Anyone else reading this likely won't know what h and v are and may not read the linked unityanswer, but that is irrelevant to the problem thankfully and they should still spot the problem easily.
Your problem is simple. You are reassigning the variable instead of adding to its existing value.
moveDirection = h * wallTransform.right;
moveDirection = v * wallTransform.up;
will set moveDirection to v * wallTransform.up;
In order to make the values add to one another, you should change it to
moveDirection += v * wallTransform.up;
Also, you don't need the if(Vector3.Dot(Camera.main.transform.forward, transform.forward) < 0)
for the vertical movement. Unless your camera can rotate such that it is upside down (beware of gimbal lock if it can), down will always be down, and otherwise the code is wrong in that instance as it should be if(Vector3.Dot(Camera.main.transform.up, transform.up) < 0)
.
So your code should look like:
// Wall Movement controls if (onWall && wallTransform) { // Lock camera for short period when transitioning moving & standing still lockCameraTimer += Time.deltaTime; if (isMoving != wasMoving) lockCameraTimer = 0.0;
//If the camera is behind/beside, right is right. Otherwise, right is left.
if(Vector3.Dot(Camera.main.transform.forward, transform.forward) < 0)
moveDirection = h * wallTransform.right;
else
moveDirection = h * -wallTransform.right;
moveDirection += v * wallTransform.up;
}
Also, I recommend that when wall-climbing, you lock the camera in your camera script to be behind the character at all times. It depends on your script, but one easy way is to do the same check as above before moving your camera:
if(Vector3.Dot(target.transform.forward, transform.forward) < 0)
//move the camera however you are moving your camera
else
//stop the camera at the closest position/angle behind/beside your target
I tried to use the code posted but I got some error saying onWall, wallTransform is not defined. But I dont what variables they are. Could you tell me what they are?
Booleans. When you see an "if" statement without anything specified (such as "onWall == 1", "onWall > 8"), it's a boolean. "if (onWall)" is the same as "if (onWall == true)". "if (!onWall)" is the same as "if (onWall == false)" (note the use of the exclamation mark).
Your answer
Follow this Question
Related Questions
Spider Wall Movement Between Waypoints 1 Answer
Climb Wall: How do I change key input from horizontal to vertical? 2 Answers
Player Climbing a Ladder? 1 Answer
Character controller wall climb mechanic in C# 0 Answers
Bombchu Mechanic 1 Answer