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 kjelle392 · Aug 14, 2014 at 02:20 PM · movementinput3dmouse

Moving a gameobject with mouse

Hello all.

I have just begun to play with Unity and trying to learn by doing small, simple projects and following tutorials. However - some of the (simplest) thing I want do to seems very hard by this point. Therefore I beg you to be kind - I promise I'll be better soon :)

Yesterday I begun a simple project which I need to make a gameobject move by mouse input. I have attached the following script to the gameobject (an excerpt):

 if(Input.GetAxis("Mouse X") < 0) {
 transform.position.x -= playerMoveSpeed * Time.deltaTime;
         //print("Moved left");
     }
     
     if(Input.GetAxis("Mouse X") > 0) {
         transform.position.x += playerMoveSpeed * Time.deltaTime;
         //print("Moved right");
     }
     
     if(Input.GetAxis("Mouse Y") > 0) {
         transform.position.z += playerMoveSpeed * Time.deltaTime;
         //print("Moved up");
     }
     
     if(Input.GetAxis("Mouse Y") < 0) {
         transform.position.z -= playerMoveSpeed * Time.deltaTime;
         //print("Moved down");
     }

This should be simple in my mind, but the gameobject don't follow the mouse! I can move the object right after starting the game, but the object is moving slower and slower and almost stop moving even with large mouse movements. The mouse pointer and objects is also not the same (the pointer start at Unitys "playbutton" above the gamescreen, but the object is on the bottom).

Can anybody give me a hint how to solve this, or tell me if it's possible at all.

Comment
Add comment · Show 5
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 christoph_r · Aug 14, 2014 at 02:29 PM 0
Share

Starting with small projects and working your way up is the right way to go about learning this. You'll be doing fine!

Now what you want to do is already a bit more advanced. To see an example of your approach, see the second piece of code here. But if you actually want the object to follow your mouse, it's a bit more tricky: You will need to raycast your mouse position into the game world. But is that what you want to do?

avatar image JusSumGuy christoph_r · Oct 10, 2018 at 05:48 AM 0
Share

He asks how to do something and you say it's to hard for you..... really? If you can't help him that's fine. But don't write a paragraph saying it's to hard. Because his write it actually is pretty simple.

avatar image kjelle392 · Aug 15, 2014 at 08:07 AM 0
Share

Yes, I suspect I have to raycast to do what I want. The cube should follow the mouse movement exactly, but clamped between the playfields borders. I have tried to "convert" the mouse position to screen coordinates using Camera.ScreenToWorldPoint with a hard-coded z-value but that value doesn't seem to ever change.

avatar image kjelle392 · Aug 16, 2014 at 07:38 AM 0
Share

It seems I've got it to work - kind of :) Posting the code here:

 function Update () {
     
     var mp : Vector3 = Input.mousePosition;
     mp = Vector3(mp.x,mp.y,transform.position.y+30);
     var sp : Vector3 = cam.ScreenToWorldPoint(mp);
     
     transform.position = Vector3(sp.x,0,sp.z);
     
     if(Input.Get$$anonymous$$ouseButtonDown(0)) {
         Instantiate(bullet,transform.position,transform.rotation);
     }
 }
avatar image robertbu · Aug 16, 2014 at 05:15 PM 0
Share

Anyone answering your question needs some critical information:

  • Is the camera directly facing positive 'z', directly facing negative 'y', or at some angle?

  • Are you using an Orthographic or a Perspective camera?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JusSumGuy · Oct 10, 2018 at 06:12 AM

It is simple. I was actually having the same problem a while back. Step 1: Make a Canvas and resize it to the scope of your main camera view. Step2 : Make an Image a blank Image and set it's alpha to 0. The image should also take the same space as the Parent Canvas. you set the alpha to 0 so it's invisble. Step3: Write this script and attach it to the image object.

using UnityEngine; using UnityEngine.EventSystems; public class MousePostioning : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler { public Transform object; public void OnPointerUp(PointerEventData data) { //---- } public void OnPointerDown(PointerEventData mouse) { object.position = Camera.main.ScreenToWorldPoint(mouse.position); } public void OnDrag(PointerEventData mouse) { object.position = Camera.main.ScreenToWorldPoint(mouse.position); } `

Then just drag and drop what ever you want to move into the object spot and your good to go. Just make sure the image and it's parent Canvas are enabled during play time.

Comment
Add comment · Show 2 · Share
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 JusSumGuy · Oct 10, 2018 at 06:18 AM 0
Share
 using UnityEngine; 
 using UnityEngine.EventSystems;
 
  public class $$anonymous$$ousePostioning : $$anonymous$$onoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {
 
  public Transform object; 
 
 public void OnPointerUp(PointerEventData data) { //---- } 
 
 public void OnPointerDown(PointerEventData mouse) 
 { 
 object.position = Camera.main.ScreenToWorldPoint(mouse.position);
  } 
 
 public void OnDrag(PointerEventData mouse)
  { 
 object.position = Camera.main.ScreenToWorldPoint(mouse.position);
  }
avatar image JusSumGuy JusSumGuy · Oct 10, 2018 at 06:20 AM 0
Share

You can put the bullet instantiation in the functions too.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Input System Can't Catch Event on Update 0 Answers

3D Movement with Gamepad with Input System? 3 Answers

Move object towards mouse in full 3D space 2 Answers

Linux and Mouse Wheel,Linux and Mouse ScrollWheel 0 Answers

How to Move a Cube 3 Answers


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