- Home /
Question by
Qinect · Feb 26, 2014 at 08:30 PM ·
mobiletouchmultitouch
How to add multitouch?
I can't move sideways and jump at the same time. I've tried some techniques, but my scripting skills are still very low. If possible just say where should the needed code go and explain a little bit. Here is my code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(CircleCollider2D))]
public class TouchControlsV2 : MonoBehaviour
{
// GUI textures
public GUITexture guiLeft;
public GUITexture guiRight;
public GUITexture guiJump;
// Movement variables
public float moveSpeed = 5f;
public float oppMoveSpeed = -5f;
public float jumpForce = 50f;
public float maxJumpVelocity = 2f;
//public float tapCount = 2;
// Movement flags
private bool moveLeft, moveRight, doJump = false;
void Update ()
{
if (Input.touchCount > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
if (guiLeft.HitTest(t.position, Camera.main))
{
moveLeft = true;
}
if (guiRight.HitTest(t.position, Camera.main))
{
moveRight = true;
}
if (guiJump.HitTest(t.position, Camera.main))
{
doJump = true;
}
}
if (t.phase == TouchPhase.Ended)
{
doJump = moveLeft = moveRight = false;
}
}
}
void FixedUpdate()
{
if (moveLeft)
{
rigidbody2D.velocity = new Vector2 (oppMoveSpeed, rigidbody2D.velocity.y);
}
if (moveRight)
{
rigidbody2D.velocity = new Vector2 (moveSpeed, rigidbody2D.velocity.y);
}
if (doJump)
{
if (rigidbody2D.velocity.y < maxJumpVelocity)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
}
else
{
doJump = false;
}
}
}
}
Comment
Best Answer
Answer by Qinect · Feb 27, 2014 at 03:57 PM
Nevermind, I fixed it.
While that may be good enough for you, can you please elaborate so that other people with the same question can solve it?