- Home /
Basic movement. Walking on walls.
Hi I've been trying to figure this one out for a few days now and I just can't do it.
I'm trying to make a robot game where the player walks on the outside of space stations. What I'm trying to do is have the robot walk along a surface but also be able to detach and float in empty space. I've made games before using the character controllers but I've found them to be severely lacking in flexibility.
I've figured out how to make the character move in space with a rigidbody and applying local forces but I can't make it stick to a surface. I've tried with raycasts getting the normal and using quaternion FromToRotation(or whatever it's called) to align my robot but it just teleports to a weird angle.
So the question: How can I make my rigidbody with cube collider align to the surface of another object and then move around on the surface of that object?
Added as a comment rather than an answer because I'm not 100% on the principles at work, but a starting point might be to decide where the "center" of gravity is on your object, trace rays to the character (or vice-versa, trace rays from the character to the center of gravity), and make the gravity direction be dependent on the angle of the ray.
However, if your world has a lot of concave areas and you want to walk perfectly upright on every single straight surface, this method falls apart.
That was also a method I had considered. $$anonymous$$y first idea was to have the robots walk on the outside of huge cylindrical "generation ships" where on the outside magnetic clamps is necessary but on the inside there'd be normal gravity due to rotation. I suppose maybe I can get the surface normal and use that vector3 as gravity. I think I may just have to take a look at the character control scripts and recreate them with my own tweaks.
BTW: I forgot to mention I work in C#
how come when i add packages i get a bunch of errors in my game and i cant play until i remove the addon i added the locomotive demmo for my controller and i didnt even apply it yet and i get a bunch of errors but the demo works fine seperatly and the locomotive addon is good for the gravity thing if you can get it to work
Hello Guys I saw this Walk on Walls script and it would be great for my Project. But I have problems with the setup.
When you say: This script does this. I created an empty object, added a box collider and a rigidbody (useGravity = false), then childed my model to it. Where do I attach the boo script to? To the character Controller or empty Object? Please can tell me someone how to setup these things?
Thanks very much
Thank you very much. ive tried to use this code for my 2D game. but it doesnt work correctly. my character cant rotate correctly on nonstraight surfaces. using UnityEngine; using System.Collections; public class character : $$anonymous$$onoBehaviour { %|435338485_1|% public float gravity= 10; // gravity acceleration %|533866359_3|% %|896307618_4|% %|291339160_5|% %|-374706575_6|% %|-1523593191_7|% %|-1400813889_8|% myNormal = transform.up; // normal starts as character up direction //GetComponent<Rigidbody2D>().freezeRotation = true; // disable physics rotation %|-658309084_11|% %|1303672388_12|% %|702750577_13|% %|1945969362_14|% %|-1039002884_15|% %|-80586187_16|% %|-933306721_17|% %|643488023_18|% %|-182794193_19|% %|1202132175_20|% %|169571728_21|% %|148407583_22|% %|-579725660_23|% %|-696494682_24|% %|1014676362_25|% %|742951143_26|% %|61964051_27|% %|-1100370671_28|% %|668280404_29|% Vector3 myForward = Vector3.Cross(transform.right, myNormal); %|1396871607_31|% %|1965830597_32|% transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, lerpSpeed * Time.deltaTime); %|-1410890088_34|% }
Answer by aldonaletto · Aug 14, 2011 at 01:00 AM
The CharacterController doesn't work in this case because it needs Y to be the normal direction. A good way to do this is to use a rigidbody (uncheck Use Gravity), and apply a local gravity in the form of a constant force opposite to the character normal direction - the character normal must be updated using a raycast to the down side.
This script does this. I created an empty object, added a box collider and a rigidbody (useGravity = false), then childed my model to it. The terrain normal is constantly determined with the downside raycast, and smoothly updates myNormal (the character normal). myNormal is used to create the local gravity, so the character is always being attracted to the surface under its feet. In order to align the character to myNormal without loosing its current forward direction, a smart trick is used (thanks to -T- for that!): the current forward is found from the cross product between transform.right and myNormal, and the desired rotation is calculated with LookRotation(new forward, myNormal) - this returns a rotation that keeps the character looking forward and with its head pointing in myNormal direction.The weight force is applied at FixedUpdate to make it strictly constant. The character moves with WS using Translate, and can even jump to its vertical direction.
ADDED FEATURE: When jump is pressed, the character casts a forward ray; if the ray hits any wall in the jumpRange distance, the character jumps and rotates nicelly to land in this wall - much like someone with magnetic boots in the outer space.
EDITED: The original algorithm used had a big problem when the character was fully upside down: it started to flip back/forth at random points, driving us crazy. Thanks to a Boo script suggested by -T-, the character now keeps its forward direction under all circumstances, and call walk on the roof or on spherical planets like expected.
var moveSpeed: float = 6; // move speed
var turnSpeed: float = 90; // turning speed (degrees/second)
var lerpSpeed: float = 10; // smoothing speed
var gravity: float = 10; // gravity acceleration
var isGrounded: boolean;
var deltaGround: float = 0.2; // character is grounded up to this distance
var jumpSpeed: float = 10; // vertical jump initial speed
var jumpRange: float = 10; // range to detect target wall
private var surfaceNormal: Vector3; // current surface normal
private var myNormal: Vector3; // character normal
private var distGround: float; // distance from character position to ground
private var jumping = false; // flag "I'm jumping to wall"
private var vertSpeed: float = 0; // vertical jump current speed
function Start(){
myNormal = transform.up; // normal starts as character up direction
rigidbody.freezeRotation = true; // disable physics rotation
// distance from transform.position to ground
distGround = collider.bounds.extents.y - collider.center.y;
}
function FixedUpdate(){
// apply constant weight force according to character normal:
rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
}
function Update(){
// jump code - jump to wall or simple jump
if (jumping) return; // abort Update while jumping to a wall
var ray: Ray;
var hit: RaycastHit;
if (Input.GetButtonDown("Jump")){ // jump pressed:
ray = Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, hit, jumpRange)){ // wall ahead?
JumpToWall(hit.point, hit.normal); // yes: jump to the wall
}
else if (isGrounded){ // no: if grounded, jump up
rigidbody.velocity += jumpSpeed * myNormal;
}
}
// movement code - turn left/right with Horizontal axis:
transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
// update surface normal and isGrounded:
ray = Ray(transform.position, -myNormal); // cast ray downwards
if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
}
else {
isGrounded = false;
// assume usual ground normal to avoid "falling forever"
surfaceNormal = Vector3.up;
}
myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
// find forward direction with new myNormal:
var myForward = Vector3.Cross(transform.right, myNormal);
// align character to the new myNormal while keeping the forward direction:
var targetRot = Quaternion.LookRotation(myForward, myNormal);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
// move the character forth/back with Vertical axis:
transform.Translate(0, 0, Input.GetAxis("Vertical")*moveSpeed*Time.deltaTime);
}
function JumpToWall(point: Vector3, normal: Vector3){
// jump to wall
jumping = true; // signal it's jumping to wall
rigidbody.isKinematic = true; // disable physics while jumping
var orgPos = transform.position;
var orgRot = transform.rotation;
var dstPos = point + normal * (distGround + 0.5); // will jump to 0.5 above wall
var myForward = Vector3.Cross(transform.right, normal);
var dstRot = Quaternion.LookRotation(myForward, normal);
for (var t: float = 0.0; t < 1.0; ){
t += Time.deltaTime;
transform.position = Vector3.Lerp(orgPos, dstPos, t);
transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
yield; // return here next frame
}
myNormal = normal; // update myNormal
rigidbody.isKinematic = false; // enable physics
jumping = false; // jumping to wall finished
}
Wow this looks amazing. I can probably translate it into C# myself, that'll also give me a chance to go through it and properly understand how and why it works.
I just tested it and it is perfect! Thank you so much. Now I can finally start creating gameplay and get on with the game.
Glad to know it helped! The worst things to convert to C# are the coroutine (JumpToWall must be declared as IEnumerator and called with StartCoroutine) and the collider.size and collider.center - you must use GetComponent to get the BoxCollider in a variable in order to use the size and center properties.
I'm using a modified form of this, lacking the JumpToWall stuff, and I notice sometimes, on certain polygons, the character will suddenly rotate 180 degress around its Y axis when trying to walk to a new polygon, effectively trapping you "in" that polygon.
I don't think I understand the turnAngle business as well as I need to, would you $$anonymous$$d explaining it more?
Look at this, this gives me exactly the behaviour I want.
public turnSpeed = 0.05 # Factor for the rotation lerp public grabDistance = 1.0 # How far away you can grab public gravity = 10.0 # How much force is applied to keep the character stuck to the surface
private isGrounded as bool private myForward as Vector3 private surfaceNormal as Vector3
def FixedUpdate(): ray as Ray hit as RaycastHit ray = Ray(transform.position, transform.up * -1)
if Physics.Raycast(ray, hit, grabDistance):
isGrounded = true
surfaceNormal = hit.normal
else:
isGrounded = false
if isGrounded and Input.GetButton("Down"):
myForward = Vector3.Cross(transform.right, surfaceNormal)
targetRotation = Quaternion.LookRotation(myForward, surfaceNormal)
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed)
rigidbody.AddForce(-gravity * surfaceNormal)
# Here, simply use AddForce() or Translate() to move around, and Rotate() to turn</pre>
Thanks, -T-, this suggestion was really great! $$anonymous$$y script was adapted to use its main idea, and now works perfectly!
Answer by getyour411 · Jan 12, 2014 at 03:25 AM
@aldonaletto Thank you for this! A big part of my game just went from conceptual to playable now that I can walk around on my "Asteroid".
PS I had to make this an answer as it exceeds the max comment size I guess.
For anyone looking for this in C#, I translated it to:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// C# translation from http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html
/// Author: UA @aldonaletto
/// </summary>
// Prequisites: create an empty GameObject, attach to it a Rigidbody w/ UseGravity unchecked
// To empty GO also add BoxCollider and this script. Makes this the parent of the Player
// Size BoxCollider to fit around Player model.
public class QM_CharController : MonoBehaviour {
private float moveSpeed = 6; // move speed
private float turnSpeed = 90; // turning speed (degrees/second)
private float lerpSpeed = 10; // smoothing speed
private float gravity = 10; // gravity acceleration
private bool isGrounded;
private float deltaGround = 0.2f; // character is grounded up to this distance
private float jumpSpeed = 10; // vertical jump initial speed
private float jumpRange = 10; // range to detect target wall
private Vector3 surfaceNormal; // current surface normal
private Vector3 myNormal; // character normal
private float distGround; // distance from character position to ground
private bool jumping = false; // flag "I'm jumping to wall"
private float vertSpeed = 0; // vertical jump current speed
private Transform myTransform;
public BoxCollider boxCollider; // drag BoxCollider ref in editor
private void Start(){
myNormal = transform.up; // normal starts as character up direction
myTransform = transform;
rigidbody.freezeRotation = true; // disable physics rotation
// distance from transform.position to ground
distGround = boxCollider.extents.y - boxCollider.center.y;
}
private void FixedUpdate(){
// apply constant weight force according to character normal:
rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
}
private void Update(){
// jump code - jump to wall or simple jump
if (jumping) return; // abort Update while jumping to a wall
Ray ray;
RaycastHit hit;
if (Input.GetButtonDown("Jump")){ // jump pressed:
ray = new Ray(myTransform.position, myTransform.forward);
if (Physics.Raycast(ray, out hit, jumpRange)){ // wall ahead?
JumpToWall(hit.point, hit.normal); // yes: jump to the wall
}
else if (isGrounded){ // no: if grounded, jump up
rigidbody.velocity += jumpSpeed * myNormal;
}
}
// movement code - turn left/right with Horizontal axis:
myTransform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
// update surface normal and isGrounded:
ray = new Ray(myTransform.position, -myNormal); // cast ray downwards
if (Physics.Raycast(ray, out hit)){ // use it to update myNormal and isGrounded
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
}
else {
isGrounded = false;
// assume usual ground normal to avoid "falling forever"
surfaceNormal = Vector3.up;
}
myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
// find forward direction with new myNormal:
Vector3 myForward = Vector3.Cross(myTransform.right, myNormal);
// align character to the new myNormal while keeping the forward direction:
Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal);
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, targetRot, lerpSpeed*Time.deltaTime);
// move the character forth/back with Vertical axis:
myTransform.Translate(0, 0, Input.GetAxis("Vertical")*moveSpeed*Time.deltaTime);
}
private void JumpToWall(Vector3 point, Vector3 normal){
// jump to wall
jumping = true; // signal it's jumping to wall
rigidbody.isKinematic = true; // disable physics while jumping
Vector3 orgPos = myTransform.position;
Quaternion orgRot = myTransform.rotation;
Vector3 dstPos = point + normal * (distGround + 0.5f); // will jump to 0.5 above wall
Vector3 myForward = Vector3.Cross(myTransform.right, normal);
Quaternion dstRot = Quaternion.LookRotation(myForward, normal);
StartCoroutine (jumpTime (orgPos, orgRot, dstPos, dstRot, normal));
//jumptime
}
private IEnumerator jumpTime(Vector3 orgPos, Quaternion orgRot, Vector3 dstPos, Quaternion dstRot, Vector3 normal) {
for (float t = 0.0f; t < 1.0f; ){
t += Time.deltaTime;
myTransform.position = Vector3.Lerp(orgPos, dstPos, t);
myTransform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
yield return null; // return here next frame
}
myNormal = normal; // update myNormal
rigidbody.isKinematic = false; // enable physics
jumping = false; // jumping to wall finished
}
}
hi, when i try to use this script i seem to just keep falling/glitching through the walls,
any reason to why?
This sound wonderfully like what i want to have (mostly... my game has a squirrel walking on trees (at all angles) ... then glide when he jumps off... Thats the hope anyways.... This might sound like a silly question... but.. How do you control your character?
It seems like it was stated up there that this doesn't play nice with character controllers... any of the default movement scripts seem to automatically add the character controller. (when i tried it with the character controller on... my test dummy flew up into the sky)
So besides an empty object with a box collider, rigid body (with gravity turned off) and the mounted "model" what else/other scripts to you use?
*** nvm I re-read it, i see you use a rigidbody movement based on force/velocity. Thanks for this question, it was exactly... what i needed for a starting point.
So... I've been trying to tweak this for a few days now and can't seem to figure out how to have my player be able to Jump off of the wall... well... tree actually. $$anonymous$$y player will be a flying squirrel, so I want him to be able to climb trees and then jump off when he wants to and then start the gliding routine... I'm currently trying to get him to just fall to the ground when he jumps off (I use a "climb button" in place of the jump when it comes to getting on a new surface) and then jump utilizes the actual jump movement.
I've been trying to have a method called JumpOffWalls() that will do similar actions as the original JumpToWall method except it won't be taking in any input fields, it will just turn the players normal, forward, rotation and such... I even had it call the jumptime() method to do the turning/rotating but the values were calculated with normal = Vector3.up
It looks like it is trying to work (and using debug logs i can see that each line gets read) and before i found a way to disable jumping twice it would work if I hit jump twice. but... by allowing jump twice, it would allow it infinitly so the player could just... "flap" up and up and up.
so... any idea how I can get this bugger to re-orient to the ground normal as soon as he jumps off?
Try storing the original value of the ground normal when the game first starts. Create a boolean for when you are climbing on walls and switch it to true when you Jump to a wall. When it comes time to jump from the tree, check to see if you are on a wall and that just the jump button had been pressed, if so, try a Quaternion.LookRotation at the ground normal you stored early, then apply gravity in that direction.
hmm... trying to get that to work... not sure how to use the look rotation. and while I can set up a bool for being on walls... (its actually quite difficult to get that working... ) but I'm actually not too concerned with discerning between jumping on ground and jumping off of the tree... either case I'm going to want the player to orient to the world normal... but he just doesn't want to do it... :/
Applying gravity in the world direction is all well and good... but I'll also need the player to turn so myNormal is the worldNormal. I was trying to use the same script lines already within this script that updates your player to orient to whatever face you are walking on, (they us the Quaternion.LookRotate) to use the worldNormal (set in the beggining as but I can't seem to make it do it to a preset normal... (as you suggested at the start) so it basically tells the script "here don't look for a surface below you, use this normal ins$$anonymous$$d to orient on) but... the script refuses... (my player does bob for a sec, so... it may be trying to reorient, but then the script overrides and maintains the surface normal it just jumped off of... so i jumps... player jumps and bobs... then falls back to the tree....
Answer by handsomePATT · Aug 15, 2011 at 03:41 PM
i know one of my friends who made a spider game literally rotated the whole level, making the walls turn into floors. not sure if its the best way, but it looked good
this was the idea of my first atempt too. But it won,t work if you work with more complex shapes than 90 degree walls. Additionaly AI and physics will be much harder and would take longer to calculate.
Answer by Julien-Lynge · Aug 13, 2011 at 08:28 PM
There's an example on the iTween site called Moving on Uneven Terrain that might be what you're looking for.
not really an answer, if you are trying to learn how to make it yourself.
Except that iTween comes with code examples that you can modify and tweak to your hearts content. If I were in his shoes, I wouldn't want to write code from scratch when there are solutions already available. I've previously downloaded several of their examples and used them as tutorials to learn how to do something.
okay, fair enough. But a lot of the questioners in here, are studients without money for licenses and I believe iTween isnt free. If you had linked to an opensource lib, I would have agreed with you. But enough said.
I certainly understand your trepidation. iTween actually is free - the only thing that costs money is the examples. It used to be that you could buy individual examples for $2 a pop, but apparently they've changed it so you have to buy all the examples together for $20.
As for me, I do the 9 to 5, so if I can find a solution for $20 and save a couple hours of program$$anonymous$$g I've saved my boss money.
Your answer
![](https://koobas.hobune.stream/wayback/20220612175658im_/https://answers.unity.com/themes/thub/images/avi.jpg)