so i want to drag and drop prefab in unity. apparently, there is a motion trail effect when the prefab is being dragged just like the gif below. and i also put the code that i tried, and i still dont know how to fix it. any solution? enter image description here
void Start()
{
// Store the original position of the prefab
originalPosition = transform.position;
// Get the ProductPrefabDestructor component attached to the spawn point
prefabDestructor = reactionProductPoint.GetComponent<ProductPrefabDestructor>();
}
void Update()
{
if (isDragging)
{
Vector3 worldPosition = GetMouseWorldPosition();
transform.position = Vector3.Lerp(transform.position, worldPosition + offset, Time.deltaTime * 10f);
}
}
void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
isDragging = true;
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // Distance from the camera
offset = transform.position - Camera.main.ScreenToWorldPoint(mousePosition);
}
}
private Vector3 GetMouseWorldPosition()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // Distance from the camera
return Camera.main.ScreenToWorldPoint(mousePosition);
}
void OnMouseUp()
{
if (isDragging)
{
isDragging = false;
if (isOverlapping && overlappingCollider != null)
{
// Destroy the previously spawned product prefab
prefabDestructor.DestroySpawnedProductPrefab();
// Spawn prefab based on overlapping collider
SpawnProductPrefab(overlappingCollider);
}
// Reset the position of the dragged prefab to its original position
transform.position = originalPosition;
}
}
New contributor
Heiden Hayz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.