- Home /
Question by
Temseii · Sep 21, 2016 at 06:42 PM ·
gameobjectvector3mouseclickdrag-and-drop
Clicking a button to make a gameobject visible and interactable?
I've got a script that allows the user to "pick up" an object and move it around. In this case it's a wall, and it's purpose is to hold back enemies that come running in. I'm trying to make it so that the wall isn't visible in the game to start with, but when you click a button it'll appear at the mouse position and is free for the user the drop where they please. Any guidance as to how this could be accomplished is very welcome!
Here's my code for dragging and dropping gameobjects.
Vector3 distance;
float posX;
float posY;
void OnMouseDown() {
distance = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - distance.x;
posY = Input.mousePosition.y - distance.y;
}
void OnMouseDrag() {
Vector3 currentPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, distance.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(currentPos);
transform.position = worldPos;
}
Comment
Your answer
Follow this Question
Related Questions
Distance interaction 0 Answers
GUI Buttons and game objects? 1 Answer
Mouse click on a game object to animate the main camera. 0 Answers
How to align the bottom of a GameObject with the ground? 1 Answer
Accessing game object based on position 2 Answers