I would simply like to open a door, but after spending 3 hours on this very simple piece of code, I have realised I need help, and it may not be a door I want to open.
In any case…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlidingLeftDoor : MonoBehaviour
{
public Transform doorLeft; // takes the transform of the door GameObject
Transform doorLeftClosed, doorLeftOpen; // sets open and closed positions, for opening then returning to being closed
public float openSpeed;
void Start()
{
// I expect I've initialised all this crap in the wrong place.
doorLeftClosed = doorLeft;
doorLeftOpen = doorLeft;
//The line below is my attempt to set the target position. Instead, it just opens the door:
doorLeftOpen.position = doorLeft.position + new Vector3(-1.3f,0f,0f);
//The line below is my next attempt...I think it equates to the same thing as the line above. It also just opens the door:
doorLeftOpen.position = new Vector3(doorLeft.position.x-1.3f,doorLeft.position.y,doorLeft.position.z);
}
void Update()
{
// The line below probably works, but I wouldn't know for sure because the door is already open by this point.
doorLeft.position = Vector3.MoveTowards(doorLeftClosed.position, doorLeftOpen.position, openSpeed*Time.deltaTime);
}
}
It’s pretty simple. All I want to do is set the MoveTowards target as doorLeftOpen. How do I do that please?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlidingLeftDoor : MonoBehaviour
{
public Transform doorLeft; // takes the transform of the door GameObject
Vector3 doorLeftClosed, doorLeftOpen; // sets open and closed positions, for opening then returning to being closed
public float openSpeed;
private Vector3 target;
void Start()
{
// I expect I've initialised all this crap in the wrong place.
doorLeftClosed = doorLeft.position;
//The line below is my attempt to set the target position. Instead, it just opens the door:
doorLeftOpen = doorLeft.position + new Vector3(-1.3f,0f,0f);
target = doorLeftOpen;
}
void Update()
{
// The line below probably works, but I wouldn't know for sure because the door is already open by this point.
doorLeft.position = Vector3.MoveTowards(doorLeft.position, target, openSpeed*Time.deltaTime);
if(target == doorLeft.position)
{
if(target == doorLeftOpen)
target = doorLeftClosed;
else
target = doorLeftOpen;
}
}
}
to be honest i dont remmember if the movetowards reachs 100% target destination so maybe you need to use aproximation. Your solution wasnt working because transform is a reference not a copy, transform is a reference but the position is a copy
and the move towards was being used as a mix of movetowards/lerp since movetowards doesnt lerp between position just adds an extra step towards the target you need to use the door transform.position as the initial pose
Hello @Altissimus,
If i understand properly, you want to move the door from it’s “Closed” Position to it’s “Open” Position.
The issue in your Start is that you define the “Closed” Transform & the “Open” Transform as being the door Transform itself (pointers), and then you change the position of the “Open” transform which therefore change the position of you door.
doorLeftClosed = doorLeft;
doorLeftOpen = doorLeft;
There are thousands or possible methods, this is one (not tested but should work if you remove typos :p) ;
public class SlidingLeftDoor : MonoBehaviour
{
public Transform doorLeft;
public float openSpeed;
// You can define the translation here it its constant, otherwise you can define it in a Start Method
private vector3 doorLeftOpen = New Vector3(-1.3f,0f,0f) // Define the position you want to move to
private bool isOpen = false; // State boolean -- For the sake of the test
private bool isEnabled = true; // State boolean -- For the sake of the test
// Then apply a generic translate method whenever you see fit
// For the sake of the example, the door will loop open - close sequence
void Update()
{
// Open
if (!isOpen && isEnable){
Translate(doorLeft, doorLeftOpen, openSpeed);
// We dont want anything to interact while door is opening/closing
StartCoroutine(OpenDoorDelay(openSpeed));
}
// Close
elseif(isOpen && isEnable){
Translate(doorLeft, -doorLeftOpen, openSpeed);
StartCoroutine(OpenDoorDelay(openSpeed));
}
}
// Define a generic method you'll use on whatever object (note that it doesnt need to be in the door class)
public void Translate(Transform myObjectTransform, Vector3 myNewRelativePosition, Float openSpeed){
// From relative to absolute position
Vector3 EndPosition = myObjectTransform.Position + myNewRelativePosition;
// Move object
myObjectTransform.position = Vector3.MoveTowards(myObjectTransform.position, EndPosition, openSpeed*Time.deltaTime);
}
// Use Corouting to delay interactions while door opening/closing -- for the example
IEnumerator OpenDoorDelay(float Speed)
{
isEnable = false; // Forbid door to be opened/closed again
yield return new WaitForSeconds(Speed);
isOpen = !isOpen ; //Switch Open/Close state
isEnable = true; // Allow door to be opened/closed again
}
}