- Home /
Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position
Hey guys! I'm making a drag-and-drop, object matching puzzle game with some moving obstacles and a countdown timer to make it a bit interesting. Anyway, what I want to happen is that when the dragged object collides with the obstacle (both colliders have kinematic rigidbodies), it will go back to the initial position and the timer will continue running.
Well, the collision kinda works but only if I restart the level, which means that the timer will start at the beginning. I tried doing transform.position = initialPosition
to the draggable object but it renders the collision useless (basically, it won't "collide" with the obstacle). I'm wondering if someone can help me with this. The codes are posted below.
Draggable Object
public class Hexagon : MonoBehaviour
{
public Transform HexagonGoal;
public float GoalOffset = 0.5f;
Vector2 initialPosition;
Vector2 mousePosition;
float xOffset, yOffset;
[HideInInspector]
public static bool Locked;
// Start is called before the first frame update
void Start()
{
initialPosition = transform.position;
}
void OnMouseDown()
{
if (!Locked)
{
xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
}
}
void OnMouseDrag()
{
if (!Locked)
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(mousePosition.x - xOffset, mousePosition.y - yOffset);
}
}
void OnMouseUp()
{
if (Mathf.Abs(transform.position.x - HexagonGoal.position.x) <= GoalOffset &&
Mathf.Abs(transform.position.y - HexagonGoal.position.y) <= GoalOffset)
{
transform.position = new Vector2(HexagonGoal.position.x, HexagonGoal.position.y);
Locked = true;
}
else
transform.position = initialPosition;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Trap" && !Locked)
{
//transform.position = initialPosition;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
Snippet of the GameController, which has the timer
// Update is called once per frame
void Update()
{
if (timerActive)
{
currentTime -= Time.deltaTime;
TimerText.text = currentTime.ToString("0");
if (currentTime <= 0)
{
currentTime = 0;
timerActive = false;
}
if (Hexagon.Locked)
{
timerActive = false;
QuestionCanvas.SetActive(true);
GameCanvas.SetActive(false);
}
}
}
The transform.position = initialPosition;
line certainly looks like it SHOULD work, assumpting the scene and its objects are configured correctly (and you un-remark it, and remove the loadscene line). I would suggest adding a
Debug.Log("Resetting position of object "+gameObject.name +" from:"+transform.position+" to:"+ initialPosition);
right before you assign the initialPosition to the transform. Just to make sure it is being called when you expect, and using the values you expect (sanity-check). Edit: or as a double sanity check.. display the transform position, AFTER you assign it also.
The Debug.Log
thing certainly works, as it shows the values where it should go back to. However, the game object doesn't return to where it should return. I think it may have something to do with On$$anonymous$$ouseDown()
or On$$anonymous$$ouseDrag()
. I'm thinking of making another bool for those functions to check if it hits the obstacle then see how it goes.
Edit: Here's what happened, the game object does return to the initial position when it hits the obstacle but then it instantly goes back to where my cursor is. Here are the changes I made:
// Reference to the traps that exist in the scene
public Transform[] Traps;
void On$$anonymous$$ouseDown()
{
//if (!Locked)
//{
// xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
// yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
//}
foreach (var trap in Traps)
{
if (!Locked)
{
if ($$anonymous$$athf.Abs(transform.position.x - trap.position.x) <= TrapOffset &&
$$anonymous$$athf.Abs(transform.position.y - trap.position.y) <= TrapOffset)
{
transform.position = initialPosition;
}
else
{
xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
}
}
}
}
void On$$anonymous$$ouseDown()
{
//if (!Locked)
//{
// xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
// yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
//}
foreach (var trap in Traps)
{
if (!Locked)
{
if ($$anonymous$$athf.Abs(transform.position.x - trap.position.x) <= TrapOffset &&
$$anonymous$$athf.Abs(transform.position.y - trap.position.y) <= TrapOffset)
{
transform.position = initialPosition;
}
else
{
xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
}
}
}
}
Think I see the problem, not sure tho.... When you set the transform.position to the initialPosition, in OnTriggerEnter2D(): you ALSO need to reset the xOffset and yOffset variables, like you do in On$$anonymous$$ouseDown().
Your answer
Follow this Question
Related Questions
Collision while dragging in Canvas with IDragHandler 0 Answers
Following instruction for drag and drop 3d object unity but not working 1 Answer
spawn and drag game object without leaving mouse button 1 Answer
multiple objects on oncollissionstay2d 1 Answer
Collision stops working at certain situations. Help? 1 Answer