Комментарии:
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);
}
}
}
this is fantastic , is possible to get a tutoral for knobs and levers?
Ответить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.
Ответить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
Ответить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!
Great! How can I add sound that can be heard once the button is pressed?
Ответитьgreat video!but how about use XR Poke Follow Affordance in 2.3 final version?
ОтветитьTakk!
ОтветитьLove your videos valem! Also here’s an idea, can you make a video on a backpack system for vr?
ОтветитьYeah Mr.Valem! Yeah button!
Ответитьmy project is Half way Done, Do You Recommend to Upgrade XR Tool kit
ОтветитьSame content every day yaaaaay
ОтветитьLet's goooo
Ответить