- Home /
Positioning problem
I'm developing a HoloLens program. I have a cylinder attached to it the following script.
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.WSA.Input;
public class Deformation : MonoBehaviour { //cylinder's position Vector3 tempPos;
//cylinder's scale
Vector3 tempScale;
IEnumerator ExecuteAfterTime(float time)
{
yield return new WaitForSecondsRealtime(time);
// Code to execute after the delay
////changing the y position to get back up
//tempPos.y += 0.2f - ((transform.position.y) / 2);
//transform.position = tempPos;
//changing the y scale to return to the original size
tempScale.y = 0.2f;
transform.localScale = tempScale;
}
private void InteractionManager_SourceUpdated(InteractionSourceUpdatedEventArgs hand)
{
//checking if the input is your hand
if (hand.state.source.kind == InteractionSourceKind.Hand)
{
//initializing hand position
Vector3 handPosition;
hand.state.sourcePose.TryGetPosition(out handPosition);
//cylinder's range
float minXRange = (transform.position.x) - ((transform.localScale.x) / 2);
float maxXRange = (transform.position.x) + ((transform.localScale.x) / 2);
float minYRange = (transform.position.y) - ((transform.localScale.y) / 2);
float maxYRange = (transform.position.y) + ((transform.localScale.y) / 2);
float minZRange = (transform.position.z) - ((transform.localScale.z) / 2);
float maxZRange = (transform.position.z) + ((transform.localScale.z) / 2);
//hand x, y and z co-ordinates
float handX = handPosition.x;
float handY = handPosition.y;
float handZ = handPosition.z;
//if my hand is on the cylinder's surface
if ((minXRange <= handX) && (handX <= maxXRange) && (minYRange <= handY) && (handY <= maxYRange) && (minZRange <= handZ) && (handZ <= maxZRange))
{
//changing the y scale to be equal to my hand position
tempScale.y = handPosition.y;
transform.localScale = tempScale;
////changing the y position to keep the cylinder's base surface on the table
//tempPos.y -= (transform.position.y) / 2;
//transform.position = tempPos;
}
else
{
StartCoroutine(ExecuteAfterTime(2f));
}
}
}
// Use this for initialization
void Start()
{
//initializing cylinder's position
tempPos = transform.position;
//initializing cylinder's scale
tempScale = transform.localScale;
InteractionManager.InteractionSourceUpdated += InteractionManager_SourceUpdated;
}
// Update is called once per frame
void Update()
{
}
}
I need the cylinder to be deformed in the y-axis responding to the position of my hand. In other words, hand co-ordinates are checked if it is on the cylinder then the cylinder is scaled in the y-axis so that its top surface be just at my hand position. Here the cylinder is scaled from both sides up and down. The cylinder is placed on a table and I need its base surface to be always on the table but when it is scaled it is no more on the table. To over come this problem I'm trying to translate the cylinder after being scaled (it is the part commented in the code) but I'm having a problem and can't do it the right way. Any help with that? Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162315im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enemy Lerping between two Vectors 1 Answer
C# sub Values/Arrays for a position Array 0 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer