x


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)?

more ▼

asked Apr 03 '10 at 08:40 AM

Adam Bruns gravatar image

Adam Bruns
274 24 27 38

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

Apr 03 '10 at 12:08 PM straydogstrut
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 17 '10 at 05:53 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

(comments are locked)
10|3000 characters needed characters left

Like straydogstrut wrote in a comment, see this question and the answers:

CharacterController falls through or slips off moving platforms

more ▼

answered Apr 03 '10 at 02:35 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

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?

Apr 03 '10 at 05:20 PM Adam Bruns

Question still needs answering, i couldnt get it to work, any help?

Apr 06 '10 at 02:53 AM Adam Bruns
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2504
x1880
x1371

asked: Apr 03 '10 at 08:40 AM

Seen: 2175 times

Last Updated: Apr 03 '10 at 08:40 AM