- Home /
Help with touch input.
Hi, I'm kinda new to unity and I am playing around with touch controls. I am making a sort of space shooting game where the player controls the character with touch input. The touch is for moving the character around. my problems is that, when I tap to a space in the screen, the character jumps onto that area that I have tapped.
here is a sample of the game : https://developer.cloud.unity3d.com/share/Wk9HwJPTMG/
Also I have made a fire button, where the player should press that in order to fire, the problem is my character jumps to where the fire button is.
Is there a way to restrict the touch to not jump around whenever the player touch other spaces of the screen?
See attached Codes and SS below.
Thank you!
Here is my code :
if (getInput())
{
Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
float x = Mathf.Clamp(rawPosition.x, -2.81f, 2.81f);
float y = Mathf.Clamp(rawPosition.y, -4.5f, Camera.main.orthographicSize);
Vector3 targetPosition = new Vector3(x, y, 0);
GetComponent<Rigidbody2D>().MovePosition(targetPosition*Time.deltaTime*40f);
//Instantiate(bullet, transform.position, Quaternion.identity);
}
else
{
Vector2 pos = transform.position;
pos.x += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
pos.y += Input.GetAxis("Vertical") * speed * Time.deltaTime;
#region Position Check
if (pos.y > Camera.main.orthographicSize)
{
pos.y = Camera.main.orthographicSize;
}
else if (pos.y <= -4.5f)
{
pos.y = -4.5f;
}
if(pos.x >= 2.81f)
{
pos.x = 2.81f;
}
else if( pos.x <= -2.81f)
{
pos.x = -2.81f;
}
#endregion
transform.position = pos;
}
bool getInput()
{
bool fire = Input.GetButton("Fire1");
bool touch = Input.touchCount > 0;
return fire || touch;
}
and here is how my game looks like
Your answer
Follow this Question
Related Questions
Standard Asset joystick has issues when using custom resolution 1 Answer
Touch input broken with multi-touch 1 Answer
[SOLVED] Touch Controls Not Responsive 1 Answer
help with touch 2 Answers
Move object with touch 3D with script 0 Answers