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
}
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.
– anon25104582That was also a method I had considered. My 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#
– XazperHello 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
– weilerAs a side note, this is NOT basic movement, it's very far from basic and even advanced movement. Especially when getting into custom character controllers.
– chubshobeThe player also has a circle collider, but it rolls much slower than I'd like, and I'm not sure how to make it go faster. The only thing I can think of is using apply force instead of velocity to move the player. Also I apologize for the extremely lackluster graphics. I wanted to get as much of the game working as possible before doing any pixel art. Here's a link to a video of what's going on: https://youtu.be/uz7e4RRQ_RI
– SolidiusJake