Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mac · Apr 08, 2011 at 07:27 PM · gameobjectinstantiatemousefollowscreentoworldpoint

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mac · Apr 08, 2011 at 07:31 PM 0
Share

Huh, auto-code formatting doesn't seem to be working properly on this post. Not good...

avatar image Eric5h5 · Apr 08, 2011 at 11:07 PM 0
Share

There is no auto code formatting; you have to select the code and hit the code format button.

avatar image Mac · Apr 08, 2011 at 11:42 PM 0
Share

Thanks. Don't see this button but after a browser refresh all the C# I pasted in is looking nicely formatted now.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges