- Home /
Ride an elevator?
In my game i have 4 pistons set up, they raise and lower themselves and you have to jump from one to another. To raise them and lower them i have this script set up:
private var startPoint : Vector3; var endPoint : Transform; var duration : float = 5.0; private var startTime: float; private var Point2: Vector3; private var position2 : boolean = false; private var number1: int = 0; private var number2: int = 0;
function Awake(){ startPoint = transform.position; Point2 = endPoint.position; startTime = Time.time; } function Update () { if(number1 ==1){ startTime = Time.time; } if(number2 ==1){ startTime = Time.time; } if(position2){ transform.position = Vector3.Lerp(Point2, startPoint, (Time.time - startTime) / duration); } else { transform.position = Vector3.Lerp(startPoint, Point2, (Time.time - startTime) / duration); } if(transform.position == Point2){ number1 +=1; number2 = 0; position2 = true; } if(transform.position == startPoint){ number2 +=1; number1 = 0; position2 = false; }
}
Well they raise and lower just fine and that works out, but if you stand on them and dont move when a piston raises you just stay at that height, until you go through it, then you fall. Though, when you move you raise up with it, is there a way to make it constantly push you up, like riding an elevator (hence the name of the question)?
Have you seen this question? http://answers.unity3d.com/questions/694/charactercontroller-falls-through-or-slips-off-moving-platforms I think it may point you in the right direction
Answer by spinaljack · Apr 17, 2010 at 05:53 PM
You can attach a raycast on the character that points down, when the hit property returns that you're on an elevator you can then update the character's position relative to the platform. The alternative would be to have the platform detect if a character is on it which may be more complicated seeing as any number of characters can be on a platform and a character can only really be on one platform at a time.
Something like this:
var platPos;
var ray = new Ray (transform.position, -vector3.up);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 5.0)) {
if(hit.collider.tag == "platform"){
platPos = hit.collider.transform.position;
}
}
Then do a lateupdate updating the character's position with the change in the platform's position.
Answer by runevision · Apr 03, 2010 at 02:35 PM
Like straydogstrut wrote in a comment, see this question and the answers:
CharacterController falls through or slips off moving platforms
Cant seem to get that to work on my FPS controller, are there any parts that are on the 2D tutorial script that i need to make this work?
Question still needs answering, i couldnt get it to work, any help?