why is there no clear answer to the previous question?
the question was towards the 5 minute into the video where the lecturer can pull the drawer in z position and the question was “HOW”? with the code he mentioned, because at that time, the position was not set to any specific position as to my understanding as well. So I am as confused as to the other person who posted the question at first. how can he move it in Z (because I cant in my project following) but I’m sure the code gets edited at the end and then perhaps it will work.
Ive checked the viedo 3 times over, I looked at my project so many times, I cant see where I did anything different from the lecturer.
But my drawerInteractable cube is the only thing moving. not the drawer itself, even though I did attatch the drawer into the drawerTransform where it should control the movement of the object?
my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class DrawerInteractable : XRGrabInteractable
{
[SerializeField] Transform drawerTransform;
[SerializeField] XRSocketInteractor keySocket;
[SerializeField] bool isLocked;
private Transform parentTransform;
private const string defaultLayer = "Default";
private const string grabLayer = "Grab";
private bool isGrabbed;
private Vector3 limitPositions;
[SerializeField] private Vector3 limitDistances = new Vector3(0.02f, 0.02f, 0f);
void Start()
{
if (keySocket != null)
{
keySocket.selectEntered.AddListener(OndrawerUnlocked);
keySocket.selectExited.AddListener(OnDrawerLocked);
}
parentTransform = transform.parent.transform;
limitPositions = drawerTransform.localPosition;
}
private void OnDrawerLocked(SelectExitEventArgs arg0)
{
isLocked = true;
Debug.Log("***DRAWER LOCKED***");
}
private void OndrawerUnlocked(SelectEnterEventArgs arg0)
{
isLocked = false;
Debug.Log("***DRAWER UNLOCKED***");
}
protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
if(!isLocked)
{
transform.SetParent(parentTransform);
isGrabbed = true;
}
else
{
ChangeLayerMask(defaultLayer);
}
}
protected override void OnSelectExited(SelectExitEventArgs args)
{
base.OnSelectExited(args);
ChangeLayerMask(grabLayer);
isGrabbed = false;
transform.localPosition = drawerTransform.localPosition;
}
void Update()
{
if(isGrabbed && drawerTransform != null)
{
drawerTransform.localPosition = new Vector3(drawerTransform.localPosition.x,
drawerTransform.localPosition.y, drawerTransform.localPosition.z);
CheckLimits();
}
}
private void CheckLimits()
{
if(transform.localPosition.x >= limitPositions.x + limitDistances.x ||
transform.localPosition.x <= limitPositions.x - limitDistances.x)
{
ChangeLayerMask(defaultLayer);
}
else if(transform.localPosition.y >= limitPositions.y + limitDistances.y ||
transform.localPosition.y <= limitPositions.y - limitDistances.y)
{
ChangeLayerMask(defaultLayer);
}
}
private void ChangeLayerMask(string mask)
{
interactionLayers = InteractionLayerMask.GetMask(mask);
}
}
Please help.
Hi there,
First, the normal response to questions can be up to 2 business days - we are not expected to answer at the weekend. Saying that we do try to answer when we can, including weekends and holidays.
The issue you have is you’re setting the drawer position to the drawer position. The drawer will never move because you’re not changing it. The code I have is the following:
drawerTransform.localPosition = new Vector3( _limitPositions.x, _limitPositions.y, transform.localPosition.z );
I hope this will help
Hi!
Thank you for the quick response, I do really appreciate it knowing you guys don’t usually respond on weekends.
It did help but I couldn’t find anywhere in the video where it was changed, also checked resources and tried comparing the git as well. Anyhow, thank you!
You are most welcome.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.