I know that’s not very descriptive but I don’t know the lingo so I’m kind of stuck.
What I’d like to do is create something that I can script off of that checks if certain conditions are met. But it can’t be a binary like ‘true or false.’ It would have to be something I can assign an ID to.
Say I have an object like an elevator and I want a button to tell it to go up or down. The game has to be able to tell whether it’s in the up or down position so when the button is hit it can move the object in the direction it needs to go until it hits its destination. So I’d have to be able to, I guess, tell the program to recognize when it has hit the “up” or “down” position.
I can’t think of any other way to start scripting this kind of thing without the scripts tripping over themselves when the object leaves the extreme up or down positions. I’ve looked into Lerps but they seem to work with direct coordinates and if the object moves from side to side I would want it to maintain it’s side to side positioning while still knowing to go from the top position to the bottom position or vice versa. The only way I can think to do this is to tell the program to create a state or ID when the object is at the bottom and consider that true until it reaches the top and then do the reverse for that.
I know that’s not the clearest way to dictate what I’m talking about but hopefully it sounds familiar to someone here.
Are you actually scripting an elevator? I’m trying to wrap my head around what you’re using this for.
In your elevator example, it could be:
int floorNumber;
if (Input.GetButtonDown("ElevatorDown")) {
// Assume we can't go lower than the first floor ...
If (floorNumber > 1) {
MoveElevatorDown();
}
}
I’m scripting something like an elevator or a platform but it can move left or right.
Imagine an elevator that can be freely moved left or right but when you hit a button it goes up to a set position or down to a set position smoothly. I can find ways to make it go straight up and down using coordinates but the issue I run into is trying to make it move up or down a certain number of units while still being able to freely control its movement back and forth.
Imagine a player is controlling a platform and they can move it along the x and z axis freely but when they hit a button the platform moves from the bottom floor to the top or from the top floor to the bottom.
Sounds like a state machine would be perfect. Your elevator can be in 4 states:
At Top
At Bottom
Moving Up
Moving Down
Each state only allows specific actions. For example
At Top: If player presses button, change state to Moving Down.
At Bottom: If player presses button, change state to Moving Up.
Moving Up: Keep moving up. When at the top, change state to At Top.
Moving Down: Keep moving down .When at the bottom, change state to At Bottom.
There are many ways to write state machines. Here’s one example. It’s not the most generically flexible way, but the code is short:
public enum State { AtTop, AtBottom, MovingUp, MovingDown }
public State state = State.AtTop;
public float speed = 5;
public float topY = 10;
public float bottomY = 0;
void Update() {
switch (state) {
case State.AtTop:
if (Input.GetButtonDown("ElevatorButton")) state = state.MovingDown;
break;
case State.AtBottom:
if (Input.GetButtonDown("ElevatorButton")) state = state.MovingUp;
break;
case State.MovingUp:
transform.position += Vector3.up * speed * Time.deltaTime;
if (transform.position.y >= topY) state = state.AtTop;
break;
case State.MovingDown:
transform.position += Vector3.down * speed * Time.deltaTime;
if (transform.position.y <= bottomY) state = state.AtBottom;
break;
}
}