A very kind and gifted programmer I know is helping me create my dream, but he is having a little trouble. I told him I would pray to the Script Gods for guidance. Please, Script Gods, hear my plea unworthy as I am to even be in this forum. I am but a lowly musician…
So, Sam Kalman is generously working on a flight script as a character controller (the player is an insect), and I’d really like it if the player could land on an object while flying, automatically transition to a traditional walking controller (walking on a wall or ceiling, for example), then take off and fly again using the flight controller.
Can someone tell me how he might be able to accomplish this? Are there any obvious barriers that will make this either overly difficult or outright impossible? Can there even be 2 controllers for one character?
Thank you all for your help. I would love to have learned to code, but I just don’t have the mind for it- I’m glad you all do.
I did something similar (i think) before. Can this not be done by getting the, non horizontal, surface to be walked on through collision with it, and then casting rays at the surface every ‘update’ to attain the normal of the surface the player is on. Then from there restraining the movement to directions that are perpendicular to the normal.
I think thats how i did it, if that makes any sense? I can go back and dig out the code from before later on if this is of any use!
var speed = 0.5;
var gravity = 50.0;
var surfaceNormal : Vector3;
var hit : RaycastHit;
// assign a sphere as your target in the Inspector
var target : Transform;
function Update ()
{
// rotate our object to align up with the target's normal
if (Physics.Raycast (transform.position, Vector3.down, hit))
{
surfaceNormal = hit.normal;
}
transform.rotation = Quaternion.FromToRotation(Vector3.up, surfaceNormal);
// still need to figure out how adjust angles south of the equator.
// At this point it won't point below the horizon.
}
function FixedUpdate()
{
// move our object
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
transform.Translate (moveDirection, Space.World);
// Apply gravity towards the center of the sphere
rigidbody.AddForce((target.transform.position - transform.position).normalized * gravity);
// When going fast enough the object will want to leave the surface if it doesn't have the right rotation (ie doesn't get the normal when south of the equator. so once you go there your rotation is messed up)
// Playing with mass, speed and gravity helps but it's a hack.
}
function Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
this isn’t exactly what you want but maybe you’ll get some ideas. it rotates your object to follow the surface normals of a target object as you move across it. it’s set up for walking around the outside of a sphere and in theory the sphere could have a non-uniform surface.
you’ll need to rework it for the inside of a room. basically you’d need to change it to add the force (fake gravity) in the opposite direction of the surface normal or maybe just away from the center of the room. right now it adds force towards the center of a target object.
i have not had time to take another look at this and i never got it working south of the equator. it won’t rotate properly if your fly needs to be upside down (ie on a ceiling). so you’d need to figure that out but hope it helps…
Thanks everyone, this helps a lot. My other problem is trying to have a 3rd-person character move between walking and flying modes. Reading pete’s script gives me some great ideas, so I’ll churn on this for a while and come back if I keep having problems. In the mean time, any other suggestions are welcome!
glad it helped. the south of the equator thing is probably along the lines of not explicitly using Vector3.up to align with the normal. iirc the problem with my script is .up is greater than zero and doesn’t like to be a negative number (that’d be .down).
for your transition thing, i’d probably look at a brief intermediate state… walking->lerp/slerp to taking off animation->lerp/slerp to flying.