Я хочу, чтобы выбранный объект сделал обмен и перемещался в его правильное положение, но другой объект перемещается в прC#

Место общения программистов C#
Ответить
Anonymous
 Я хочу, чтобы выбранный объект сделал обмен и перемещался в его правильное положение, но другой объект перемещается в пр

Сообщение Anonymous »

using UnityEngine;

public class DragAndSwap : MonoBehaviour
{
private Camera cam;
private GameObject selectedObject;
private GameObject collidedObject;
private Vector3 offset;
private Vector3 originalPosition;
private Vector3 collidedOriginalPosition; // Store the original position of the collided object

void Start()
{
cam = Camera.main;
}

void Update()
{
if (Input.GetMouseButtonDown(0)) // Left mouse button click
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit)) // Check if we hit an object
{
selectedObject = hit.collider.gameObject;
originalPosition = selectedObject.transform.position; // Save original position

Vector3 screenPoint = cam.WorldToScreenPoint(selectedObject.transform.position);
offset = selectedObject.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}

if (Input.GetMouseButton(0) && selectedObject) // While dragging
{
Vector3 screenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.WorldToScreenPoint(selectedObject.transform.position).z);
Vector3 newWorldPosition = cam.ScreenToWorldPoint(screenPoint) + offset;
selectedObject.transform.position = new Vector3(newWorldPosition.x, newWorldPosition.y, originalPosition.z); // Keep Z position fixed
}

if (Input.GetMouseButtonUp(0) && selectedObject) // When released
{
if (collidedObject != null && collidedObject.CompareTag("Draggable"))
{
// Swap positions
Vector3 tempPosition = collidedObject.transform.position;
collidedObject.transform.position = originalPosition; // Move collided object to the original position of the selected object
selectedObject.transform.position = tempPosition; // Move selected object to the collided object's position
}
else
{
// No collision -> Move back to original position
selectedObject.transform.position = originalPosition;
}

selectedObject = null;
collidedObject = null; // Reset the collided object after swap or reset
}
}

void OnTriggerEnter(Collider other)
{
if (selectedObject != null && other.gameObject.CompareTag("Draggable"))
{
collidedObject = other.gameObject; // Store reference to the object we collided with
collidedOriginalPosition = collidedObject.transform.position; // Store the original position of the collided object
}
}

void OnTriggerExit(Collider other)
{
if (collidedObject == other.gameObject)
{
collidedObject = null; // Reset if the selected object moves away
}
}
}
< /code>
Я хочу, чтобы выбранный объект сделал обмен и перемещался в ее правильное положение, но другой объект перемещается в правильное положение. Выбранный объект после обмена с другим объектом не перемещается в положение поменяемой, пока он перемещается в исходное положение. Пожалуйста, помогите, что я делаю не так в этом коде? < /P>
Я попытался решить его шаг за шагом. Я мог бы подумать, что проблема может быть в выбранном оргенде Органальное положение неправильно хранится, а другой Raeson может быть функцией триггера, не является правильным вызовом?

Подробнее здесь: https://stackoverflow.com/questions/794 ... on-but-the
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»