01-09-2024 12:52 PM - edited 01-09-2024 01:06 PM
Could someone please review my code? I am going mad from this.
What I am trying to do is to move the World object to the hand but offset by some distance so the hand doesn't end up inside of the object. I got the movement right where it Lerps to the position of the RightHandAnchor/LeftHandAnchor, but can't for the life of me figure out what is wrong with shifting the destination by a certain distance.
What currently happens is that when I release the DistanceHandGrab, the object moves to my hand and it ends up inside the object. I know that the HandAnchor is following the hand, since the object always ends up over the hand no matter from where I grab it. I just am not able to move the target position and it is driving me insane. Please help.
public float distanceFromAnchor = 10;
void HandleDistanceHandGrabEnd(GameObject endedObject)
{
// Save the World's new position
previousWorldPosition = endedObject.transform.position;
previousScale = World.transform.localScale.x;
// Create a dictionary to store the original positions of the children
Dictionary<Transform, Vector3> originalPositions = new Dictionary<Transform, Vector3>();
// Iterate over each child of the World
foreach (Transform child in World.transform)
{
// Save the child's original position
originalPositions[child] = child.position;
}
// Set the World's position to the endedObject's position
World.transform.position = endedObject.transform.position;
// Iterate over each child of the World again
foreach (Transform child in World.transform)
{
// Set the child's position to its original position
child.position = originalPositions[child];
}
// Calculate the direction from the hand to the object that was grabbed
Vector3 directionFromHandToEndedObject = endedObject.transform.position - headAnchor.transform.position;
// Normalize this direction to get a unit vector
Vector3 unitDirectionFromHandToEndedObject = directionFromHandToEndedObject.normalized;
// Multiply this unit vector by the distance you want to move the object away from the hand
Vector3 shift = unitDirectionFromHandToEndedObject * distanceFromAnchor;
// Add this to the hand's position to get the new target position
Vector3 targetPosition = RightHandAnchor.transform.position + shift;
// Move the World to the target position
StartCoroutine(MoveWorld(targetPosition, targetScale));
}
Here is code for Moving the World
IEnumerator MoveWorld(Vector3 targetPosition, float targetScale)
{
// Define the duration for the scaling operation
float duration = 0.3f;
// Get the start time of the scaling operation
float startTime = Time.time;
while (Time.time < startTime + duration)
{
// Calculate how far along the duration we are (between 0 and 1)
float t = (Time.time - startTime) / duration;
// Interpolate the scale and position of the World
World.transform.localScale = Vector3.one * Mathf.Lerp(previousScale, targetScale, t);
World.transform.position = Vector3.Lerp(World.transform.position, targetPosition, t);
yield return null; // Wait for the next frame
}
// Ensure the World ends up at exactly the target scale and position
World.transform.localScale = Vector3.one * targetScale;
World.transform.position = targetPosition;
}