how to make a game object follow the mouse cursor
How do I get it to where I make a game object stays under the cursor. No other answers are clear so please help.
edit: I also want to know if it could be in a grid like area so instead of being free move, it snaps
Please make an attempt and post a new question if you find you are truely stuck.
I've already looked, I usually don't just post questions before looking online for a solution.
Glad to hear it.
However there is a certain lack of attempt in this question.
Ill give you the benefit of the doubt. Post something that you have tried, or this question will need to be closed.
Answer by Firedan1176 · Oct 04, 2015 at 09:23 PM
There's many approaches to how you want to do this, and it all depends on what you want to achieve. You can make your object follow your mouse without depending on a Z coordinate, and you could also have your object be aligned to another object (such as a ground), if the mouse is over that object.
First method
You can use Camera.main.ScreenToWorldPoint(Input.MousePosition)
to get your mouse position in world space as a Vector3. Instead of using that exactly, try using this:
float zAxis = 2f;
Vector3 mousePosition;
void Update() {
mousePosition = Camera.main.ScreenToWorldPoint(Input.MousePosition);
mousePosition.z = zAxis;
//If you get an error with the above line, replace it with this:
//mousePosition = new Vector3(mousePosition.x, mousePosition.y, zAxis);
}
When you use ScreenToWorldPoint
to get mouse position, the Z axis will either be really strange, or 0.
You can then use the Vector3 mousePosition
to set the transform.position
of another object to where the mouse cursor is in world space.
Second Method
If you have a floor in your scene, you could also raycast from your mouse cursor to plant your object on the ground.
Try using this code to achieve this:
RaycastHit hit;
Ray ray;
void Update() {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, Mathf.Infinity) {
transform.position = hit.point;
}
}
You can place this code on an object to make it align to the mouse when the mouse is over any object. This is untested, so I'm unsure of any errors. Also keep in mind that any object will be placed on your ground at the center/pivot point; For example, Unity's primitive cube will be halfway into the ground.
Im going with method 2 since Im using a floor, but how can I do it so when I click a GUI button, an object then follows the cursor
You will need to use the OnClick() field of Unity's UI buttons to call your own function, and only do the updating from my above script if a bool is set true. You can set this bool to true once you call a function in the OnClick field as mentioned. Look at this.
If my answer above answered your question, please accept it as an answer.
I've managed to fix the problems with the script, and now I am getting an error saying "NullReferenceException: Object reference not set to an instance of an object Follows$$anonymous$$ouse.Update () (at Assets/Scripts/Follows$$anonymous$$ouse.cs:17)" I cannot add a picture of the error because of unity answers not letting me
Post it on http://postimage.org/ and link it. Its all we can do until functionality is fixed.
FYI, the if line (7) should read like so:
if(Physics.Raycast(ray, out hit, Mathf.Infinity)) {
Your answer
Follow this Question
Related Questions
OnTriggerStay2D when trigger is spawned on object not moving 1 Answer
Destroy specific object with tag 2 Answers
Instantiate Previous Prefab in Array 0 Answers
I guys I have a really hard question 0 Answers