Pushable Button with Unity XR Interaction

Pushable Button with Unity XR Interaction

Valem Tutorials

1 год назад

21,066 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

3DAction
3DAction - 17.06.2023 13:21

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class ButtonFollowVisual : MonoBehaviour
{
public Transform visualTarget;
public Vector3 localAxis;
public float resetSpeed = 5;
public float followAngleThreshold = 45;

private bool freeze = false;

private Vector3 initialLocalPos;

private Vector3 offset;
private Transform pokeAttachTransform;

private XRBaseInteractable interactable;
private bool isFollowing = false;

// Start is called before the first frame update
void Start()
{
initialLocalPos = visualTarget.localPosition;

interactable = GetComponent<XRBaseInteractable>();
interactable.hoverEntered.AddListener(Follow);
interactable.hoverExited.AddListener(Reset);
interactable.selectEntered.AddListener(Freeze);
}

public void Follow(BaseInteractionEventArgs hover)
{
if(hover.interactorObject is XRPokeInteractor)
{
XRPokeInteractor interactor = (XRPokeInteractor)hover.interactorObject;

isFollowing = true;

pokeAttachTransform = interactor.attachTransform;
offset = visualTarget.position - pokeAttachTransform.position;

float pokeAngle = Vector3.Angle(offset, visualTarget.TransformDirection(localAxis));

if(pokeAngle > followAngleThreshold)
{
isFollowing = false;
freeze = true;
}
}
}

public void Reset(BaseInteractionEventArgs hover)
{
if(hover.interactorObject is XRPokeInteractor)
{
isFollowing = false;
freeze = false;
}
}

public void Freeze(BaseInteractionEventArgs hover)
{
if(hover.interactorObject is XRPokeInteractor)
{
freeze = true;
}
}

// Update is called once per frame
void Update()
{
if(freeze)
return;

if(isFollowing)
{
Vector3 localTargetPosition = visualTarget.InverseTransformPoint(pokeAttachTransform.position + offset);
Vector3 constrainedLocalTargetPosition = Vector3.Project(localTargetPosition, localAxis);

visualTarget.position = visualTarget.TransformPoint(constrainedLocalTargetPosition);
}
else
{
visualTarget.localPosition = Vector3.Lerp(visualTarget.localPosition, initialLocalPos, Time.deltaTime * resetSpeed);
}
}
}

Ответить
Nick Frame
Nick Frame - 14.06.2023 17:09

this is fantastic , is possible to get a tutoral for knobs and levers?

Ответить
ZElectricitE
ZElectricitE - 01.06.2023 01:35

For some reason my button visual is floating in the air to the right when I start the game, when I remove the Lerp section it stops.

Ответить
Wyatt Murray
Wyatt Murray - 19.05.2023 04:57

i am seeing trouble regarding the event listeners. you do not have anything in "hoverEntered" yet you add a listener to it? I am attempting to make this button on a wall, and i cannot get the button to even detect when i hover, select, or exit hovering. im currently trying to flip it on its side and reproduce your exact video, but it, so far, has not being working to my expectations. I am currently using unity 22.2.1f1, with the most up to date XR toolkit download available

Ответить
Joel Bellucci
Joel Bellucci - 07.04.2023 20:18

Another fantastic tutorial, Valem. Merci!

For the very last part, I found that I had to change the code slightly to prevent the cube from moving if I poked it from below. Instead of...

if(pokeAngle < followAngleThreshold)
{
isFollowing = true;
freeze = false;
}
}

I went with....

if(pokeAngle > followAngleThreshold)
{
isFollowing = false;
freeze = true;
}
}

And that worked for me.

Thanks again, Valem!

Ответить
Nika
Nika - 28.03.2023 15:00

Great! How can I add sound that can be heard once the button is pressed?

Ответить
XR技术研习社
XR技术研习社 - 01.03.2023 05:50

great video!but how about use XR Poke Follow Affordance in 2.3 final version?

Ответить
Jostein Schefte
Jostein Schefte - 28.02.2023 00:24

Takk!

Ответить
Vertigo
Vertigo - 27.02.2023 02:41

Love your videos valem! Also here’s an idea, can you make a video on a backpack system for vr?

Ответить
Raphaël
Raphaël - 26.02.2023 21:22

Yeah Mr.Valem! Yeah button!

Ответить
Fazbear Entertainment
Fazbear Entertainment - 26.02.2023 20:28

my project is Half way Done, Do You Recommend to Upgrade XR Tool kit

Ответить
Klavishnica
Klavishnica - 26.02.2023 20:28

Same content every day yaaaaay

Ответить
No Signal
No Signal - 26.02.2023 20:13

Let's goooo

Ответить