i3e-asg2/Assets/Scripts/spaceshipSlidingdoor.cs
2024-07-01 11:11:08 +08:00

112 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.PlasticSCM.Editor.WebApi;
using Unity.VisualScripting;
using UnityEngine;
public class SlideDoor : Interactable
{
bool IsOpen = false;
bool opening=false;
[SerializeField]
private GameObject RightDoor;
Vector3 RightDoorStart;
Vector3 RightDoorEnd;
[SerializeField]
private GameObject LeftDoor;
[SerializeField]
private float LerpSpeed;
public void Start()
{
RightDoorStart=RightDoor.transform.position;
Debug.Log(RightDoorStart);
RightDoorEnd= RightDoor.transform.position;
RightDoorEnd.z += 2;
Debug.Log(RightDoorEnd);
// RightDoorEnd = RightDoor.transform.position;
// RightDoorEnd.z += 2;
// RightDoorEnd = RightDoor.transform.position;
// Debug.Log(RightDoorEnd);
// RightDoorStart = RightDoor.transform.position;
// Debug.Log(RightDoorStart);
// LeftDoorEnd = LeftDoor.transform.position;
// LeftDoorEnd.z += 2;
// LeftDoorEnd = LeftDoor.transform.position;
// LeftDoorStart = LeftDoor.transform.position;
// Debug.Log("done ini");
}
void Update()
{
if(opening)
{
}
}
void SlideRight()
{
if (!IsOpen)
{
float time = 0;
while (time < 1)
{
RightDoor.transform.position = Vector3.Lerp(RightDoorStart, RightDoorEnd, time);
time += Time.deltaTime * LerpSpeed;
}
// Vector3 currentPosition = RightDoor.transform.position;
// currentPosition.z += 2;
// RightDoor.transform.position = currentPosition;
}
else
{
float time = 0;
while (time < 1)
{
RightDoor.transform.position = Vector3.Lerp( RightDoorEnd,RightDoorStart, time);
time += Time.deltaTime * LerpSpeed;
}
// Vector3 currentPosition = RightDoor.transform.position;
// currentPosition.z -= 2;
// RightDoor.transform.position = currentPosition;
}
}
void SlideLeft()
{
if (!IsOpen)
{
Vector3 currentPosition = LeftDoor.transform.position;
currentPosition.z -= 2;
LeftDoor.transform.position = currentPosition;
}
else
{
Vector3 currentPosition = LeftDoor.transform.position;
currentPosition.z += 2;
LeftDoor.transform.position = currentPosition;
}
}
public override void Interact()
{
opening=true;
// SlideRight();
// SlideLeft();
if (IsOpen)
{
IsOpen = false;
}
else
{
IsOpen = true;
}
}
}