How can I use both mouse and keyboard to do the same thing? [SOLVED]
Hello!
I've just finished the Breakout Game tutorial and I want to move my paddle object with both keyboard and mouse. I want the player to choose which one he/she wants.
public float paddleSpeed = 1f;
private Vector3 playerPos = new Vector3 (0, -9.9f, 0);
// Update is called once per frame
void Update () {
float xPos = transform.position.x + (Input.GetAxis ("Mouse X") * paddleSpeed);
playerPos = new Vector3 (Mathf.Clamp (xPos, -7.5f, 7.5f), -9.9f, 0f);
transform.position = playerPos;
}
}
I've discovered how to move with my mouse but how can I add 'Input.GetAxis ("Horizontal")' into the script? And how can I hide my mouse pointer?
Answer by Positive7 · Sep 04, 2015 at 10:58 PM
using UnityEngine;
public class asd : MonoBehaviour
{
public float paddleSpeed = 1f;
private Vector3 playerPos = new Vector3(0, -9.9f, 0);
bool mouseMoves;
void Start() {
Cursor.visible = false;
}
void Update()
{
float m = Input.GetAxis("Mouse X");
float h = Input.GetAxis("Horizontal");
float[] t = {m,h};
for (int i = 0; i < t.Length; i++) {
if (t[0] != 0)
{
mouseMoves = true;
float xPos = transform.position.x + t[0] * paddleSpeed;
playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
transform.position = playerPos;
}
else if(t[1] != 0 && !mouseMoves)
{
float xPos = transform.position.x + t[1] * paddleSpeed;
playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
transform.position = playerPos;
}
else
{
mouseMoves = false;
}
}
}
}
or Simply :
using UnityEngine;
public class asd : MonoBehaviour
{
public float paddleSpeed = 1f;
private Vector3 playerPos = new Vector3(0, -9.9f, 0);
bool mouseMoves;
void Start()
{
Cursor.visible = false;
}
void Update()
{
if (Input.GetAxis("Mouse X") != 0)
{
mouseMoves = true;
float xPos = transform.position.x + Input.GetAxis("Mouse X") * paddleSpeed;
playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
transform.position = playerPos;
}
else if (Input.GetAxis("Horizontal") != 0 && !mouseMoves)
{
float xPos = transform.position.x + Input.GetAxis("Horizontal") * paddleSpeed;
playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
transform.position = playerPos;
}
else
{
mouseMoves = false;
}
}
}
$$anonymous$$an thanks a lot! This is what I was looking for but now my object starts at the middle of the screen. I want it to start at Vector3 (0, -9.9f, 0) and move from there in X-Axis. How can I do that?
[UPDATE]
Never$$anonymous$$d. I just figured it out:
void Start()
{
Cursor.visible = false;
transform.position = new Vector3(0, -9.9f, 0);
}
Thank you!
void Start()
{
transform.position = playerPos;
Cursor.visible = false;
}
Wow, thanks, m8. This helped and learned me a lot. Here is your adjusted code for 2D movement:
private void $$anonymous$$anage$$anonymous$$ovement() {
float mX = Input.GetAxis("$$anonymous$$ouse X");
float h = Input.GetAxis("Horizontal");
float mY = Input.GetAxis("$$anonymous$$ouse Y");
float v = Input.GetAxis("Vertical");
float[] t = { mX, h, mY,v };
for (int i = 0; i < t.Length; i++) {
if (t[0] != 0 || t[2] != 0) {
mouse$$anonymous$$oves = true;
float xPos = transform.position.x + t[0] * $$anonymous$$ovementSpeed;
float yPos = transform.position.y + t[2] * $$anonymous$$ovementSpeed;
playerPos = new Vector3($$anonymous$$athf.Clamp(xPos, $$anonymous$$usX$$anonymous$$ovementConstrait, plusX$$anonymous$$ovementConstrait),
$$anonymous$$athf.Clamp(yPos,$$anonymous$$usY$$anonymous$$ovementConstrait, plusY$$anonymous$$ovementConstrait));
transform.position = playerPos;
} else if ((t[1] != 0 || t[3] != 0) && !mouse$$anonymous$$oves) {
float xPos = transform.position.x + t[1] * $$anonymous$$ovementSpeed;
float yPos = transform.position.y + t[3] * $$anonymous$$ovementSpeed;
playerPos = new Vector3($$anonymous$$athf.Clamp(xPos, $$anonymous$$usX$$anonymous$$ovementConstrait, plusX$$anonymous$$ovementConstrait),
$$anonymous$$athf.Clamp(yPos,$$anonymous$$usY$$anonymous$$ovementConstrait,plusY$$anonymous$$ovementConstrait));
transform.position = playerPos;
} else {
mouse$$anonymous$$oves = false;
}
}
}
I dont even know how comes that arrow keys are registered for movement. Its some default thing?
Your answer
Follow this Question
Related Questions
Input.GetAxis going crazy when I click outside the game window 0 Answers
Can someone help me rewriting my script from transform.position to transform.Translate? 1 Answer
Movement scrip 0 Answers
How to avoid jittering / stuttering when colliding with objects in 3D? 2 Answers
Moving walls in multiple places 1 Answer