- Home /
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.
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
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;
}
}
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.
it worked but i want Lerp the position between object to mouse position.
Thanks!!
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 )
thank you sir ;) it worked. I only changed Get$$anonymous$$eyDown to just Get$$anonymous$$ey.
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?
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
spelling :
private var mousposX : float;
var yourObject : Transform;
function Update () {
mouseposX = Input.mousePosition.x;
yourObject.position.x = mouseposX;
}
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.
You could change the Update function to a FixedUpdate.
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;
}