I’ve got a cabinet with 6 drawers which each drawer having this script attached to it. Can someone tell me if they see any problems with it cause it just isn’t working. I have my distance to be opened on the z axis, but it doesn’t move at all. The function is being called from the interface and is actually being called. I’ve also debugged each if statement and that works aswell.
public class Drawer : MonoBehaviour, IInteractable {
public float distanceToBeOpened;
private Vector3 closedPos;
private Vector3 openPos;
private bool isClosed;
// Use this for initialization
public void Start(){
isClosed = true;
closedPos = this.transform.position;
openPos = new Vector3 (this.transform.position.x, this.transform.position.y, this.transform.position.z + distanceToBeOpened);
}
public void Interact(){
Debug.Log ("Trying to Interact");
if (isClosed) {
transform.position = openPos;
isClosed = false;
}
else{
transform.position += closedPos;
isClosed = true;
}
}
}
Yeah sorry I just put the += afterwards just to check I wasn’t doing something ridiculous, it was a last ditch effort, despite the fact that I believe that just transform.position = closedPos would work fine.
I have the average value set to -0.4f which should be fine. I’ve set that in the inspector for each drawer as some are slightly open. Not one opens, it just seems really strange. I’ve also debugged the bool isClosed aswell and it changes accordingly with each call.
This also assumes that your object is aligned to the world Z axis since you’re only adding to Z position. I would expect that you would want to be offsetting in the local Z axis, which may involve all 3 positional axes potentially.
So if you manually change it in the inspector, .4f moves it enough? (it’s just a really small value is why I ask). Maybe even do something drastic and add 100 to it. Just to see what happens.
Also maybe print out transform.position in each if statement to see if it changes. Just some things to try.
Also note that the inspector may be showing local position and not world. It should still move by your code.
Interesting. Do me a favor and run with these debug statements. Make sure you’re not calling Interact twice for some reason. Make sure you have your console not collapsing values otherwise you’d miss a duplicate debug statement perhaps.
If those values output in the console make sense and only output once per interaction, then something outside of this script is affecting the movement.
Hmmm… strange. The positional coordinates are actually changing. They read (-1, 0.6, 12.7) for old position and (-0.6, 0.6, 12.7) for the new position so it’s definitely moving them, it just doesn’t move them on my game or scene window?
Wow, It’s just moving the collider and not the gameobject, By chance I’ve just seen it in the scene window while debugging in the game window. Is this normal? I thought transform.position would relate to the gameobject and not the collider?
The thing is, My mesh renderer is attached to this child object, aswell as the box collider and the script. It’s the child I want to move yet it only moves then collider by the looks of it