Answer found elsewhere
Make an object move in place of the mouse X and Y [UPDATE]
I am working on a game where you can build in blocks. I want the player to be able to see the outline of the block they are about to place down before they place it so that they can see what it will look like.
To do this I am wanting to replace the mouse with the block or have a block follow the mouse's X and Y so that it is always in the same position as the mouse and not lagging behind.
I have looked for a solution but the block is never fixed to the mouse or the block floats up to the camera and on the example I found I could not work out how to stop it doing so,
Thanks in advance.
EDIT:
I have go the block to follow the mouse perfectly with the following script:
Vector3
screenPoint,
offset,
scanPos,
curPositionx,
curPositiony,
curScreenPoint;
public float
gridSize = 1;
void Awake() {
scanPos = gameObject.transform.position;
screenPoint = Camera.main.WorldToScreenPoint(scanPos);
offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void Update() {
curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPositionx = Camera.main.ScreenToWorldPoint (curScreenPoint);
curPositiony = Camera.main.ScreenToWorldPoint (curScreenPoint);
curPositionx.x = (float)(Mathf.Round (curPositionx.x) / gridSize);
curPositiony.y = (float)(Mathf.Round (curPositiony.y) / gridSize);
transform.position = curPositionx;
transform.position = curPositiony;
}
}
However it glides Smoothly on the grid and does not snap to the grid.
If you remove the " curPositiony " and the lines of code that are with it:
curPositiony = Camera.main.ScreenToWorldPoint (curScreenPoint);
curPositiony.y = (float)(Mathf.Round (curPositiony.y) / gridSize);
transform.position = curPositiony;
Then the X axis will snap but the Y axis still wont.
Answer by toddisarockstar · Jan 14, 2016 at 08:12 PM
i did something like this with a past project. is the game three dimentional? do the the blocks "snap" to a position or do you need smooth movement on X and Y? if it is 3D, do the blocks stack? your answer is going to come from "raycasting" from camera to mouse position. but with a couple answers i can give code example.
Hi @toddisarockstar the game is 3D and I am wanting it to be able to snap on a grid. I have a grid but I felt that the mouse needed a visual box before i tried to do snapping as I would not be able to see if it worked or not.
Follow this Question
Related Questions
Why is my MouseLook script not letting me look up? 0 Answers
Convert game to Android 2 Answers
CATCH MOUSE CLICK EVENT ON OBJECTS 0 Answers