Creating elevators in UnityNov-17-08, 4:49 pm by HanfordFile under: Video games, consoles, Wii, Unity, tech Unity's Character Controller is an example of what makes Unity great. It's a part of Unity's engine that is specifically designed for creating moving characters in video games. It has built-in support handling slopes, walls, and steps, all rolled into a single component so that you don't have to (re)write this for every game, like you would in most general purpose 3d engines. But all that built-in functionality comes at a price: if you want to make your character do something that the built-in stuff doesn't support, then you gotta roll it yourself. Still, it's better than having to roll everything yourself. One of the limitations of the Character Controller is that it does not support elevators and moving platforms very well. The Character Controller will work fine with platforms when they're not moving, but if they are moving, the character controller will not stick to them. This was the first real shortcoming I've run into while using Unity, so it was really my first hurdle I had to jump. After a bit of Googling around I confirmed that it indeed was a shortcoming of the Character Controller. Some suggested workarounds, but I couldn't find a good solution with code snippets posted anywhere. In one of the posts I found about it, someone suggested building platforms with triggers above them that would parent the object to the elevator itself. I liked the idea of using parenting, but I wanted a solution that was built into my character itself, and didn't require triggers placed above surfaces on the elevators themselves. So I wrote one. My Elevator SolutionNote: As I'm just getting started with Unity, I'm attaching code snippets here that should be complete, but I may have left out variable declarations. I stripped out all the unrelated code to make reading this example easier.This solution is part of a script that you'd attach to your character GameObject. Your character should have a character controller attached to it. The code will parent itself to any object it touches that has the tag "elevator" on it. It will unattach itself when you move off of it. You don't need to add a trigger to your elevator; you just need to make sure it has a non-trigger collider on it. I've used a bit of code taken from the Unity docs to check to see if the object is below the character, so that it won't attach itself if it bumps into the sides of elevators and whatnot, but that shouldn't be a big problem anyway if your character has gravity applied to it. How to implement itThe heart of it is two functions: one is the OnControllerColliderHit function, and one that should be called immediately after every Move you make with your character. Declare these at top of your script: private var onelevator = 0; // this st used to keep track of whether or not you're parented to the elevator private var touchingelevator = 0; // this is a flag used to determine whether or not you're actually touching the elevator (regardless of parented state). Here's the code to call whenever you move a character: touchingelevator = 0; // Set this to zero before EVERY character controller Move() you make. var charcon = GetComponent(CharacterController); // You don't need to do it this way; it's just the way I tend to do it var flags = charcon.Move(moveDirection * Time.deltaTime); // Here's your character Move() postMoveCheck(); // call this after EVERY Move() you make. Here's functions you need to include in the script:
function OnControllerColliderHit (hit : ControllerColliderHit)
{// This function gets called every time your character collides with an object, before Move() returns.
//We use this to check to see if we're touching the elevator.
var body : Rigidbody = hit.collider.attachedRigidbody;
if (hit.moveDirection.y > -0.3)
return;
if (hit.gameObject.tag != "elevator")
return;
// At this point we know we're sitting on top of an elevator. :)
touchingelevator = 1; // This is used to track whether or not we touched the elevator THIS UPDATE.
// We do it this way because this function doesn't get called when we are not touching the elevator, and we need some
// way to know that so that so we can unparent once we're not touching it anymore.
if (!onelevator)
{ // this moves the character into the elevator's hiearchy
// should only happen once, after collision.
transform.parent = hit.gameObject.transform; // Make us a parent of the elevator. Nach.
onelevator = 1;
}
}
function postMoveCheck ()
{// This checks to see if we're on an elevator anymore, and if we aren't, we clear the parent.
// Call this immediately after moving the character.
if (!touchingelevator && onelevator)
{
transform.parent = null;
onelevator = 0; // We're not on the elevator anymore! ;)
}
}
Comments and suggestions are always welcome. Additionally, if you have a different solution to this problem, I've love to hear about it. Contact me or post in the comments below. Feedback - 4 responsesDisplayed newest to oldest. Leave a comment.delaware insurance administration wrote: 2 days ago, 10:44 pm Nice blog getting great info. some times many of us neglect this type of items & also go through a whole lot at the same time. aetna health insurance quotes individual wrote: 2 days ago, 10:36 pm Great products from you, man. Ive review your goods previous to plus you may be equally well astounding. I love precisely what youve got the following, like just what you could be saying and ways in which a person point out them. You earn this engaging therefore you even now find a way to help in keeping them smart. I actually find it difficult to wait to go through added on your part. That is definitely really a terrific blog page. Charles Hinshaw wrote: Dec-22-08, 6:55 am There are also some physics-based characters controllers on the wiki. The main issue with things like this is that the default controller isn't. Leave a commentComments are displayed on posts and visible to all site visitors. |
|
I make things. From consumer electronics, to video games, to theme park attractions. Perhaps I can make things for you! Check out my portfolio. When I'm not making things for other people, I'm usually experimenting.
Follow me on Twitter. Message me on Facebook. Email me using my contact form.
|
|
I actually entirely go along with the aforementioned thoughts, the web has been a doubt increasing in to the biggest moderate of connection across the globe as well as its due to sites this way of which tips are distribution hence swiftly.