- Home /
Moving VR Button in LocalSpace with only one axis Unlocked
Hey Everyone
I'm trying to move this button along its local z position using a collider on my vr hands, finger. It has a box collider and a Rigid body. I am trying to constrain the movement of the local X and Y axis using code, but when I do I have to leave the Local X open, so that it can move proper or if I don't it moves along the global path, when the X is open the user would be able to push the button side to side which is not ideal.
I'm Using this code to constrain the button:
public bool lockX;
public bool lockY;
public bool lockZ;
public float returnSpeed;
protected Vector3 startPosition;
void Start()
{
// Remember start position of button
startPosition = transform.localPosition;
}
// Update is called once per frame
void Update()
{
ContrainButton();
// Return button to startPosition over time
transform.localPosition = Vector3.Lerp(transform.localPosition, startPosition, Time.deltaTime * returnSpeed);
}
private void ContrainButton()
{
//Lock Axis of checked Bool
Vector3 localPos = transform.localPosition;
if (lockX && localPos.x != startPosition.x) localPos.x = startPosition.x;
if (lockY && localPos.y != startPosition.y) localPos.y = startPosition.y;
if (lockZ && localPos.z != startPosition.z) localPos.z = startPosition.z;
transform.localPosition = localPos;
}
} I've tried google search and read through the forums but couldn't find much to fit my specific problem Any help to get me on the right path would be great, Thank you everyone :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
GoogleVR handling Close button in Stereo mode 2 Answers