- Home /
Question by
ikonino · Jul 11, 2020 at 04:17 PM ·
2dtower defense
how to make an object go back to its starting position if it doesen't hit a target?
Hello! i am making a tower defense game, and i ran into a problem, if i drag my turrets and they don't hit a slot, i want them to go back to its starting position, how do i do that?
here is the code i'm using: public class DragDrop : MonoBehaviour { private bool isDragging;
public void OnMouseDown()
{
isDragging = true;
}
public void OnMouseUp()
{
isDragging = false;
}
void Update()
{
if(isDragging)
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.Translate(mousePosition);
}
}
}
thank you <3
Comment
Answer by Z_Y_X · Jul 11, 2020 at 04:56 PM
if you mean this:
public float needDistance = 0.1f;
Vector3 startPosition;
Vector3 mousePosition;
void Start()
{
startPosition = transform.position;
}
void OnMouseUp()
{
if(Vector3.Distance(transform.position, mousePosition)<needDistance)
{
transform.position = startPosition;
}
isDragging = false;
}
...