- Home /
how do i drag a clone object using gesture?
Hi everyone, our project is about using gesture. we are new to unity and only know c# , now we want to make the clone object able to drag.However, end up the original object able to drag but clone object cannot. I think we unable to achieve it because we don't know the name of the clone object and don't know how to call it. Can anyone give me an idea? Thanks a lot!
This is the script for clone obejct
void OnSelect() {
furniture = (GameObject)Resources.Load(furnitureName, typeof(GameObject));
BoxCollider bc = furniture.AddComponent<BoxCollider>() as BoxCollider;
bc.center = new Vector3(0F, 0F, 0F);
bc.size = new Vector3(0.01F, 0.01F, 0.01F);
int i = 0;
if(furnitureName == "Sofa")
{
//(+ forward, + up, + left)
furniture.transform.position = new Vector3(-3.2F, 1.8F, 7.5F);
furniture.transform.eulerAngles = new Vector3(1.349f, 178.41f, 1.213f);
furniture.transform.localScale = new Vector3(0.2213F, 0.3970f, 0.1865f);
}
Instantiate(furniture);
}
private Vector3 GetHandPivotPosition()
{
throw new NotImplementedException();
}
This is Script for dragging
public void StartDragging() { if (!IsDraggingEnabled) { return; }
if (isDragging)
{
return;
}
// Add self as a modal input handler, to get all inputs during the manipulation
InputManager.Instance.PushModalInputHandler(gameObject);
isDragging = true;
//GazeCursor.Instance.SetState(GazeCursor.State.Move);
//GazeCursor.Instance.SetTargetObject(HostTransform);
Vector3 gazeHitPosition = GazeManager.Instance.HitInfo.point;
Vector3 handPosition;
currentInputSource.TryGetPosition(currentInputSourceId, out handPosition);
Vector3 pivotPosition = GetHandPivotPosition();
handRefDistance = Vector3.Magnitude(handPosition - pivotPosition);
objRefDistance = Vector3.Magnitude(gazeHitPosition - pivotPosition);
Vector3 objForward = HostTransform.forward;
// Store where the object was grabbed from
objRefGrabPoint = mainCamera.transform.InverseTransformDirection(HostTransform.position - gazeHitPosition);
Vector3 objDirection = Vector3.Normalize(gazeHitPosition - pivotPosition);
Vector3 handDirection = Vector3.Normalize(handPosition - pivotPosition);
objForward = mainCamera.transform.InverseTransformDirection(objForward); // in camera space
objDirection = mainCamera.transform.InverseTransformDirection(objDirection); // in camera space
handDirection = mainCamera.transform.InverseTransformDirection(handDirection); // in camera space
objRefForward = objForward;
// Store the initial offset between the hand and the object, so that we can consider it when dragging
gazeAngularOffset = Quaternion.FromToRotation(handDirection, objDirection);
draggingPosition = gazeHitPosition;
// GlobalVar.previpos = handPosition;
StartedDragging.RaiseEvent();
}
private void UpdateDragging() { Vector3 newHandPosition; currentInputSource.TryGetPosition(currentInputSourceId, out newHandPosition);
Vector3 pivotPosition = GetHandPivotPosition();
// GlobalVar.previpos = pivotPosition;
Vector3 newHandDirection = Vector3.Normalize(newHandPosition - pivotPosition);
newHandDirection = mainCamera.transform.InverseTransformDirection(newHandDirection); // in camera space
Vector3 targetDirection = Vector3.Normalize(gazeAngularOffset * newHandDirection);
targetDirection = mainCamera.transform.TransformDirection(targetDirection); // back to world space
float currenthandDistance = Vector3.Magnitude(newHandPosition - pivotPosition);
float distanceRatio = currenthandDistance / handRefDistance;
float distanceOffset = distanceRatio > 0 ? (distanceRatio - 1f) * DistanceScale : 0;
float targetDistance = objRefDistance + distanceOffset;
draggingPosition = pivotPosition + (targetDirection * targetDistance);
GlobalVar.destPost = draggingPosition;
if (IsOrientTowardsUser)
{
draggingRotation = Quaternion.LookRotation(HostTransform.position - pivotPosition);
}
else
{
Vector3 objForward = mainCamera.transform.TransformDirection(objRefForward); // in world space
draggingRotation = Quaternion.LookRotation(objForward);
}
// Apply Final Position
HostTransform.position = draggingPosition + mainCamera.transform.TransformDirection(objRefGrabPoint);
HostTransform.rotation = draggingRotation;
if (IsKeepUpright)
{
Quaternion upRotation = Quaternion.FromToRotation(HostTransform.up, Vector3.up);
HostTransform.rotation = upRotation * HostTransform.rotation;
}
}
//Assign the color from gazeablecolorpicker.cs to draw ^
/// <summary>
/// Stops dragging the object.
/// </summary>
public void StopDragging()
{
if (!isDragging)
{
return;
}
// Remove self as a modal input handler
InputManager.Instance.PopModalInputHandler();
if (firstTime)
{
GlobalVar.previpos = GlobalVar.position;
firstTime = false;
}
isDragging = false;
currentInputSource = null;
//This will draw when it meet the start position and end position
//GameObject myLine = new GameObject();
//myLine.transform.position = GlobalVar.previpos;
//myLine.AddComponent<LineRenderer>();
//LineRenderer lr = myLine.GetComponent<LineRenderer>();
//lr.material = new Material(Shader.Find("Particles/Additive"));
//lr.SetColors(c1, c2);
//lr.SetWidth(0.1f, 0.1f);
//lr.SetPosition(0, GlobalVar.previpos);
//lr.SetPosition(1, GlobalVar.destPost);
//GlobalVar.previpos = GlobalVar.destPost;
StoppedDragging.RaiseEvent();
}
Your answer
Follow this Question
Related Questions
How to detect if an object is being moved back and forth by the user? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer
Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers