[Solved]Adding Left/Right mouse movement

Trying to get my mouse hooked up to the player object so I can move it with the mouse left and right… here’s what I have. It doesn’t work.

void MoveWithMouse()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    ship.transform.Translate(speed, 0f, 0f);

}

I’ve also tried:

void MoveWithMouse()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    ship.transform.Translate(moveHorizontal, 0f, 0f);

}

I don’t think the “Horizontal” is mapped to the mouse by default. You could change that in the…I might not get this exactly right but I think it’s edit->preferences->input manager, or something like that. It opens in the Inspector, where you can click a little arrow to expand “Axis” and then change the values of the Horizontal axis.

Or you can use the specific mouse inputs (Mouse X and Mouse Y or names similar to that) instead of Horizontal.

1 Like

@Todd_Vance thank you sir. I’ve been trying to figure out the simplest way to attach mouse movement. I will look at this. I was half asleep when I wrote that last night.

I like to have the mouse as well as the keyboard so this is what I did:

Add: public bool MoveWithMouse;

then add these lines to your "void Update:

if(Input.GetMouseButtonDown(0)){
InvokeRepeating (“Fire”, .000001f, firingRate);
}

if(Input.GetMouseButtonUp(0)){
CancelInvoke(“Fire”);
}

if(MoveWithMouse){
float moveHorizontal = Input.GetAxis (“Mouse X”);
ship.transform.Translate(moveHorizontal, 0f, 0f);
}

That has worked great for me. Hopefully this will save others lots of time! Have fun and happy coding!

1 Like

I tried doing this the same way as we did the paddle in Block Breaker, but the mouse arrow is way to the left of the ship. Does this have to do with PPU?