- Home /
Choppy y-axis crane movement when grabbing food, not dropping food on target
Hi guys,
I have a crane in my game that tracks an item of food across Unity's x and z axes (y is for vertical movement, after all). It also grabs said food item when you press a special grab button. When you press the Play button, it then takes the food towards the target - but it does not drop the food near the target like it should. Here is the Update() method of CraneBehavior.cs
void Update () {
Vector3 newPos;
Vector3 foodPos = food.transform.position;
if (!clawActivated)
{
// Track the food at a fixed height
newPos = new Vector3(foodPos.x, foodPos.y + 2.0f, foodPos.z);
}
else
{
Debug.Log("Moving claw...");
// Make the claw go down to meet the food over a period of time, then pick it up
newPos = new Vector3(foodPos.x, foodPos.y + 2.0f * Time.deltaTime, foodPos.z);
if (newPos.x == targetPos.x && newPos.z == targetPos.z)
{
if (newPos.y != foodPos.y)
{
newPos.y -= (2.0f * Time.deltaTime);
if (newPos.y <= foodPos.y);
{
newPos.y = foodPos.y;
}
}
else
{
// Move food with crane.
if (!collectedFood)
{
// Only execute this code if the claw is not near the destination
if (!overDestination)
{
collectedFood = true;
Vector3 newFoodPos = transform.position - new Vector3(0.0f, 1.5f, 0.0f);
foodPos = newFoodPos;
}
}
}
}
// Move to target when food is grabbed
if (collectedFood)
{
// Movement code
Vector3.MoveTowards(newPos, targetPos, 1.0f);
// Drop the food on the kitten
if (transform.position == targetPos - offsetFromTarget)
overDestination = true;
}
}
transform.position = newPos;
}
The x, y, and z-axis tracking of the food is smooth and satisfactory, but when I press the grab button, the crane simply moves to a lower position (to grab the food) in an incredibly choppy manner. It also does not release the food over the target, meaning that the food isn't dropping from the crane. Any code suggestions?
MachCUBED
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613092400im_/https://answers.unity.com/themes/thub/images/avi.jpg)