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
1
Question by Buster617 · Mar 22, 2012 at 04:42 PM · movementmousemouseposition

Getting object to follow mouse position along X axis only

I am trying to move my character along the X axis using my mouse cursor. I have searched around and found some scripts such as http://www.unifycommunity.com/wiki/index.php?title=Click_To_Move

However, these are all moving the object to the exact position of the mouse, at the moment, I have it so that it moves up when the spacebar is pressed and falls to the ground when it isn't and i'd like to keep this for the Y axis movement, but get the object to follow my mouse's X axis co-ordinates for the objects movement horizontally. But I can't figure out a way to do this without the object moving to the mousePosition Y and X co-ordinates.

Any help would be appreciated.

Comment
Add comment · Show 1
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 falconer · Oct 22, 2014 at 05:51 AM 0
Share

This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here

$$anonymous$$ove to Touch / Click Position - The Game Contriver

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Mar 23, 2012 at 02:47 PM

I did something similar for someone recently , here is that question : fire-at-mouse-position-fps

Here is a script re-written for your purpose. Make sure you read it, the raycast is what is doing the work , and answering your question ( as suggested by @BY0LOG1C ).

 private var mouseposX : float;
 private var rayHitWorldPosition : Vector3;
 var yourObject : Transform;
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         // raycast
         var rayHit : RaycastHit;
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), rayHit))
         {
             // where did the raycast hit in the world - position of rayhit
             rayHitWorldPosition = rayHit.point;
                 print ("rayHit.point : " + rayHit.point + " (rayHitWorldPosition)");
             mouseposX = rayHit.point.x;
         }
         yourObject.position.x = mouseposX;
     }
 }


Comment
Add comment · Show 4 · 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 AlucardJay · Mar 24, 2012 at 03:34 AM 0
Share

I should also mention that you need a surface for the raycast to hit. I assumed you already had terrain or floor of some kind.

avatar image ganesh.pingale · Jan 23, 2013 at 02:11 PM 0
Share

it worked but i want Lerp the position between object to mouse position.

Thanks!!

avatar image AlucardJay · Jan 23, 2013 at 02:15 PM 0
Share

Then it would be something like

 yourObject.position.x = $$anonymous$$athf.Lerp( yourObject.position.x, mouseposX, rateOfLerp );

of course you would have to declare and assign a variable for rateOfLerp =]

(wow, April last year, some of these answers I don't remember doing now! I would name some variables differently now, eg. mouseposX should be called mouseWorldPosX )

avatar image maroltl · Nov 16, 2013 at 07:30 PM 0
Share

thank you sir ;) it worked. I only changed Get$$anonymous$$eyDown to just Get$$anonymous$$ey.

avatar image
0

Answer by Joshua4missions · Nov 16, 2013 at 07:39 PM

Wouldn't adding a rigid body and constraining it to the X coordinate be easier?

Comment
Add comment · 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
0

Answer by pkkr42 · Mar 22, 2012 at 05:03 PM

i´m also new to unity and stuff, but what you can try to do is taking only the x position of the mouse and set it to the objects transform.position.x

 privater var mousposX;
 var yourObject: Transform;
 function Update () {
  
 mouseposX = Input.mousePosition.x;
 yourObject.Position.x = mouseposX;
 
 
 }

i´m not sure if this helps you but maybe it will work with something like this

Comment
Add comment · Show 4 · 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 AlucardJay · Mar 22, 2012 at 05:13 PM 0
Share

spelling :

 private var mousposX : float;
 var yourObject : Transform;
 function Update () {
     mouseposX = Input.mousePosition.x;
     yourObject.position.x = mouseposX;
 }
avatar image Buster617 · Mar 23, 2012 at 01:52 PM 0
Share

I tried this and Camera.main.ScreenToWorldPoint and neither are working properly.

 private var mouseposX : float;
 var yourObject : Transform;
 function Update () {
 mouseposX = Input.mousePosition.x;
 yourObject.position.x = mouseposX;
 }

The object moves, however it appears to lag and isn't in the position of the mouse, i move my mouse to the right and the object moves to the right, but my mouse is at the left side of the screen while the object is in the middle of the screen. But this is the closest so far.

avatar image Working_Robot Buster617 · Jun 04, 2016 at 04:10 PM 0
Share

You could change the Update function to a FixedUpdate.

avatar image AlucardJay · Mar 23, 2012 at 02:46 PM 0
Share

I would follow the advice on @BY0LOG1C 's answer.

avatar image
0

Answer by by0log1c · Mar 22, 2012 at 05:33 PM

You'll need to convert the mouse position from screen area to world position, using something like Camera.ScreenToWorldPoint() , this exact code is untested but I'Ve used this before IIRC:

 function Update():void
 {
     myObject.transform.position.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
 }


Comment
Add comment · 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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get general direction of mouse/tap location relative to player on mouse click? 0 Answers

How do I stop an object from cloning when moving? 1 Answer

How to move GameObject by mouseclick? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me with my TPS Controller/Camera? 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