- Home /
Continually Updating position of Instantiated Object (to Mouse Pointer)
Hi all. I am having incredible difficulty with getting an object to follow my mouse pointer around. My goal with the prototype is to:
1.) Click on a GUI button to spawn an object into game world -- DONE 2.) Spawned object is placed at the coordinates of the current Mouse position -- PARTIAL: only using a constant Z-axis value so it is not dynamic to current camera position 3.) Each frame, update the X and Y of the object to match that of the Mouse while keeping the object in view of the camera (say, 5 units?) -- INCOMPLETE
There's obviously more I want to accomplish based off this interaction but I'd like to keep this concise. So far I've been floundering around with methods like ScreenPointToRay and other things slightly out of my depth and I'm hoping a kind soul out there can nudge this novice scripter in the proper direction:
using UnityEngine; using UnityEditor; using System.Collections;
public class ElementsHUD : MonoBehaviour {
public Vector3 spawnPosition; public Ray ray; public RaycastHit hit; private Vector3 mousePos;
private float mouseX; private float mouseY;
void Update () {
// Update Mouse Pointer position on EACH frame mousePos = Input.mousePosition; //Track mouse position as Vector mouseX = Input.mousePosition.x; //Track mouse x-axis (float) mouseY = Input.mousePosition.y; //Track mouse y-axis (float) //spawnPosition = new Vector3(mouseX, mouseY, 1); //Set default spawn position of AirNode
ray = camera.ScreenPointToRay(new Vector3(mouseX, mouseY, camera.nearClipPlane)); Debug.Log("Current mouse position = " + ray);
if (mouseAttach == true) { if (AirNode) { spawnPosition = new Vector3(mouseX, mouseY, 2); } }
/* AirNode.position... ??? <-- How Do I just manipulate the AirNode's X,Y & Z? */
}
// Display buttons for the 4 elements void OnGUI () {
GUILayout.BeginArea (new Rect (Screen.width/2 - 200, Screen.height/2 + 100, 300, 300)); GUILayout.Box (textOutput); GUILayout.EndArea ();
if (GUI.Button(new Rect(100, 300, 100, 20), new GUIContent("Air"))) { elementSelect = elementAir; textOutput = "You selected the " + elementSelect + " node."; Debug.Log("Air Node Selected");
mouseAttach = true;
GameObject objAir = (GameObject)Instantiate(AirNode, spawnPosition, transform.rotation);
AirNode.rigidbody.isKinematic = true;
} }
}
Yes, sorry this is a mouthful of clunky-ass code but hopefully some of you can discern what I'm trying to do here. :) Perhaps using the ScreenToWorldPoint method might be more appropriate? Not sure, this problem has given me a slight headache at this point.
Thanks to anyone who can offer up some suggestions or flat-out sample code. :D
Huh, auto-code formatting doesn't seem to be working properly on this post. Not good...
There is no auto code formatting; you have to select the code and hit the code format button.
Thanks. Don't see this button but after a browser refresh all the C# I pasted in is looking nicely formatted now.
Your answer
Follow this Question
Related Questions
getting .Transform from instanntiated object 2 Answers
Object instantiates in the same spot (ScreenToWorldPoint) 0 Answers
mouse Input Instantiate 0 Answers
How do I make prefab follow a gameobject?(C#) 1 Answer
Problem with trigger 1 Answer