I’m trying to Rotate an Any door in my scenes by code. I need to rotate a door smoothly, but it happens instantly (without Rotating).I used the Lerp function, but nothing changed. I have two scripts. The first one is attached to the door.. It has two functions: DoorOpen and DoorClose. Here is my code.
<code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
[SerializeField] float speed;
[SerializeField] float openAngle;
[SerializeField] float closeAngle;
Quaternion targetOpenRotation;
Quaternion targetCloseRotation;
// Start is called before the first frame update
void Start()
{
targetOpenRotation = Quaternion.Euler(transform.rotation.y, openAngle, transform.rotation.z);
targetCloseRotation = Quaternion.Euler(transform.rotation.y, closeAngle, transform.rotation.z);
}
public void DoorOpen()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetOpenRotation, Time.deltaTime * speed);
}
public void DoorClose()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetCloseRotation, Time.deltaTime * speed);
}
}
</code>
<code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
[SerializeField] float speed;
[SerializeField] float openAngle;
[SerializeField] float closeAngle;
Quaternion targetOpenRotation;
Quaternion targetCloseRotation;
// Start is called before the first frame update
void Start()
{
targetOpenRotation = Quaternion.Euler(transform.rotation.y, openAngle, transform.rotation.z);
targetCloseRotation = Quaternion.Euler(transform.rotation.y, closeAngle, transform.rotation.z);
}
public void DoorOpen()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetOpenRotation, Time.deltaTime * speed);
}
public void DoorClose()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetCloseRotation, Time.deltaTime * speed);
}
}
</code>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
[SerializeField] float speed;
[SerializeField] float openAngle;
[SerializeField] float closeAngle;
Quaternion targetOpenRotation;
Quaternion targetCloseRotation;
// Start is called before the first frame update
void Start()
{
targetOpenRotation = Quaternion.Euler(transform.rotation.y, openAngle, transform.rotation.z);
targetCloseRotation = Quaternion.Euler(transform.rotation.y, closeAngle, transform.rotation.z);
}
public void DoorOpen()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetOpenRotation, Time.deltaTime * speed);
}
public void DoorClose()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetCloseRotation, Time.deltaTime * speed);
}
}
The second code I use for the purpose of Raycasting. is attached to the player.
<code>using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
public class Interaction: MonoBehaviour
{
bool doorOpened;
private void Start()
{
doorOpened = false;
}
void Update()
{
//Ray from the center of screen
Vector3 RayStartPosition = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray cameraRay = Camera.main.ScreenPointToRay(RayStartPosition);
//Distance to open a door
float maxDistance = 3f;
if (Physics.Raycast(cameraRay, out RaycastHit hit, maxDistance))
{
string objectTag = hit.collider.gameObject.tag;
//Door open and close
if (objectTag == "Door" && Input.GetKeyDown(KeyCode.E))
{
var door = hit.collider.gameObject.GetComponent<Door>();
if (doorOpened)
{
door.DoorClose();
doorOpened = !doorOpened;
}
else
{
door.DoorOpen();
doorOpened = !doorOpened;
}
}
}
}
}
</code>
<code>using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
public class Interaction: MonoBehaviour
{
bool doorOpened;
private void Start()
{
doorOpened = false;
}
void Update()
{
//Ray from the center of screen
Vector3 RayStartPosition = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray cameraRay = Camera.main.ScreenPointToRay(RayStartPosition);
//Distance to open a door
float maxDistance = 3f;
if (Physics.Raycast(cameraRay, out RaycastHit hit, maxDistance))
{
string objectTag = hit.collider.gameObject.tag;
//Door open and close
if (objectTag == "Door" && Input.GetKeyDown(KeyCode.E))
{
var door = hit.collider.gameObject.GetComponent<Door>();
if (doorOpened)
{
door.DoorClose();
doorOpened = !doorOpened;
}
else
{
door.DoorOpen();
doorOpened = !doorOpened;
}
}
}
}
}
</code>
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
public class Interaction: MonoBehaviour
{
bool doorOpened;
private void Start()
{
doorOpened = false;
}
void Update()
{
//Ray from the center of screen
Vector3 RayStartPosition = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray cameraRay = Camera.main.ScreenPointToRay(RayStartPosition);
//Distance to open a door
float maxDistance = 3f;
if (Physics.Raycast(cameraRay, out RaycastHit hit, maxDistance))
{
string objectTag = hit.collider.gameObject.tag;
//Door open and close
if (objectTag == "Door" && Input.GetKeyDown(KeyCode.E))
{
var door = hit.collider.gameObject.GetComponent<Door>();
if (doorOpened)
{
door.DoorClose();
doorOpened = !doorOpened;
}
else
{
door.DoorOpen();
doorOpened = !doorOpened;
}
}
}
}
}
I’ve tried to use Lerp and Slerp, but nothing changed.