- Home /
Drag objects uisng the minimap
So for a university project, I'm creating an application which allows users to place instantiate objects (furniture, etc.) and move them around a room in first person. I've included a topdown minimap on screen which allows the user to see where they're placing objects, but ideally I'm also wanting to give the user the ability to move the items by clicking on them via the minimap and dragging them around.
My minimap is an orthographic camera positioned above the player (and a child of the player, so it follows them), being rendered onto a renderTexture. My first thought is to somehow translate mouse position within the rect of the minimap into a real world position of the camera and to raycast to see whether an item is being clicked on; then if so, allow the user to change that object's x and z position.
But I'm having issues using raycasting on a camera that isn't Camera.main, and if I'm honest, not entirely sure how to convert the mouse position on the minimap to the real-world coordinates.
Here is the code I've been experimenting with so far, but couldn't get the DrawRay to show. This is attached to the minimap and is triggered by EventTrigger when the mouse clicks down.
function Update(){
if(ray != null){
Debug.DrawRay(ray.origin, Vector3.down, Color.red);
}
}
function OnMouseDown(){
var miniMapRect = this.gameObject.GetComponent.<RectTransform>().rect;
var screenRect = new Rect(
this.gameObject.transform.position.x,
this.gameObject.transform.position.y,
miniMapRect.width, miniMapRect.height);
var mousePos = Input.mousePosition;
mousePos.y -= screenRect.y;
mousePos.x -= screenRect.x;
ray = minimapCam.ScreenPointToRay(mousePos);
Debug.DrawRay(ray.origin, Vector3.down, Color.red);
}
I've only starting using Unity in the last 6 months so I'm still making a lot of mistakes. Any help, or a push in the right direction would be much appreciated. Thanks.
Your answer
Follow this Question
Related Questions
Raycasting problems 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Clicking stops working when viewing WebPlayer games 0 Answers
Path finding using Raycasting inside of Unity(JavaScript) 1 Answer
Make A Laser? 1 Answer