Need help with my code
Hey everyone. Could anyone help me with this script? It's supposed to be an elevator script and it kinda works, but it's very buggy I can't understand why. The first script I put on a button, which represents a position where the elevator must go.
public float MaxRange {get{return maxRange;}}
private const float maxRange = 100f;
public customElevator Elevator;
public GameObject OuterDoorLeft;
public GameObject OuterDoorRight;
public Vector3 LeftDoorTarg;
public Vector3 LeftDoorPos;
public Vector3 RightDoorPos;
public Vector3 RightDoorTarg;
private void Awake()
{
LeftDoorPos = OuterDoorLeft.transform.localPosition;
LeftDoorTarg.x = LeftDoorPos.x+0.35f;
RightDoorPos = OuterDoorRight.transform.localPosition;
RightDoorTarg.x = RightDoorPos.x-0.35f;
}
public void OnInteract()
{
Elevator.ActivateElevator(this);
}
public void OnStartHover()
{
}
public void OnEndHover()
{
}
Second script goes on the elevator itself and moves it when a button is pressed. It also deals with door opening and here's the problem. The doors act strange, the speed varies and it seems like the try to open and close at the same time, stuttering. What could be the reason?
public float MaxRange {get{return maxRange;}}
private const float maxRange = 100f;
public GameObject InnerDoorLeft;
public GameObject InnerDoorRight;
bool move = false;
bool arrived = false;
bool DoorsOpened = false;
public float speed=1f;
public float doorSpeed=1f;
Vector3 TargetFloor;
interButton floor;
public void ActivateElevator(interButton floor){
if(floor!=null){
this.floor = floor;
move = true;
TargetFloor = floor.transform.position;
}
}
private void FixedUpdate() {
if(DoorsOpened==true){
StartCoroutine(CloseDoors());
}
if (move==true && DoorsOpened==false)
{
MoveElevator();
}
if(move==false && arrived==true){
OpenDoors();
}
}
public void MoveElevator(){
transform.position = Vector3.MoveTowards(transform.position, TargetFloor, speed*Time.deltaTime);
if(transform.position == TargetFloor){
move=false;
arrived = true;
}
}
public void OpenDoors(){
floor.OuterDoorLeft.transform.localPosition = Vector3.MoveTowards(floor.OuterDoorLeft.transform.localPosition, floor.LeftDoorTarg, doorSpeed*Time.deltaTime);
floor.OuterDoorRight.transform.localPosition = Vector3.MoveTowards(floor.OuterDoorRight.transform.localPosition, floor.RightDoorTarg, doorSpeed*Time.deltaTime);
if(floor.OuterDoorLeft.transform.localPosition == floor.LeftDoorTarg && floor.OuterDoorRight.transform.localPosition == floor.RightDoorTarg){
arrived = false;
DoorsOpened = true;
}
}
public IEnumerator CloseDoors(){
yield return new WaitForSeconds(3);
floor.OuterDoorLeft.transform.localPosition = Vector3.MoveTowards(floor.OuterDoorLeft.transform.localPosition, floor.LeftDoorPos, doorSpeed*Time.deltaTime);
floor.OuterDoorRight.transform.localPosition = Vector3.MoveTowards(floor.OuterDoorRight.transform.localPosition, floor.RightDoorPos, doorSpeed*Time.deltaTime);
if(floor.OuterDoorLeft.transform.localPosition == floor.LeftDoorPos && floor.OuterDoorRight.transform.localPosition == floor.RightDoorPos){
DoorsOpened = false;
}
}
There's also a script on my camera and an IInteractable interface but they just deal with my interaction system.
I apologize for the terrible code I'm still pretty new to c# and Unity. Hope someone can help me and thanks in advance.
Your answer
Follow this Question
Related Questions
Update() and FixedUpdate() not running on C# scripts 0 Answers
FixedUpdate and Update not working in script 0 Answers
Why does FixedUpdate work when Update() doesn't? 2 Answers
Optimizations: Should I use Update or FixedUpdate? Could you help me optimize rotations? 1 Answer
Start() and Update() execution issue 6 Answers