- Home /
Multi Touch Drag Screen To Move Player?
Hello. it's been a little while since I've last ask a question on here. I've been busy. Anyways I was messing around with touch controls and i was wondering if it's possible to have multi touch controls, to where if I drag my finger to the left, the Player moves left, and if I drag it to the right, the Player moves right. I've seen people directly grab the object and drag it, but I don't want to do that. I want to just drag the screen for the Player to move, and if I stop the Player will stop.
Right now my Player movements are set to the default arrow keys, so think of it as that. I also have a jump control to where you tap on any part of the screen and the Player jumps, so that's why this needs to be multi touch or else it will affect the jump controls. By the way i'm testing controls so nothing is finalized yet, so feel free to suggest different ways of moving my Player. One way I already tried was virtual Joystick movement, I haven't got it working yet. Anyways thanks for reading i hope you guys/gals can help.
Here's a little part of my Character Controller script showing some things related to movement and jump:
public float defaultSpeed = 5f;
public float maxSpeed = 10f;
public float speed = 5f;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
if (!grounded)
return;
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Update()
{
if (grounded && Input.GetMouseButtonDown(0)) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
AudioSource.PlayClipAtPoint(JumpAudio[ Random.Range(0, JumpAudio.Length) ], transform.position, .6f);
}
}
}
Anyone else have any ideas? This topic seems to be pretty vague.
Answer by orb · Sep 12, 2014 at 08:53 AM
Since you're saying "multi touch" I'm assuming you mean two or more fingers.
What you're looking for first then is Input.touchCount, which tells you how many fingers are currently touching the screen. Input.GetTouch(0) and Input.GetTouch(1) will give you the first two, and the deltaPosition property on each will give you the amount of movement. You can check Input.GetTouch(0).phase for the TouchPhase.Moved enum to verify that it actually HAS moved.
So a change you need to do to regular controls is to wrap it in a test for Input.touchCount == 1, for instance. Or a switch. Supporting three-finger special movement may also be useful.
With the new game controller support in iOS I think the second joystick on a typical gamepad would be the third and fourth joystick axis in the input options, but this is something I haven't had the opportunity to play with.
Well I said multi touch because my jump happens when I touch any part of the screen. So I want to move the Player and be able to jump at the same time.
Now that I think about it, the virtual joystick really would be the best option for me. The problem is, is that I can't get the joystick to only move my Player left and right. Do you know how I can achieve this? Here's my controller that controls the virtual joystick:
public class Controller : $$anonymous$$onoBehaviour
{
public CNJoystick movementJoystick;
private Transform transformCache;
// Use this for initialization
void Awake()
{
if (movementJoystick == null)
{
throw new UnassignedReferenceException("Please specify movement Joystick object");
}
movementJoystick.FingerTouchedEvent += Start$$anonymous$$oving;
movementJoystick.FingerLiftedEvent += Stop$$anonymous$$oving;
movementJoystick.Joystick$$anonymous$$ovedEvent += $$anonymous$$ove;
transformCache = transform;
}
// You can extend this class and override any of these virtual methods
protected virtual void $$anonymous$$ove(Vector3 relativeVector)
{
// It's actually 2D vector
transformCache.position = transformCache.position + relativeVector;
Face$$anonymous$$ovementDirection(relativeVector);
}
private void Face$$anonymous$$ovementDirection(Vector3 direction)
{
if (direction.sqr$$anonymous$$agnitude > 0.1)
{
transform.up = direction;
}
}
protected virtual void Stop$$anonymous$$oving()
{
}
protected virtual void Start$$anonymous$$oving()
{
}
}
The transformCache.position = transformCache.position + relativeVector; is what moves my Player.
relativeVector is an x and y coordinate, so it would move left, right, up and down. If you simple refactor that to include just a float parameter for horizontal movement and making a new Vector2(horizontal, 0f) it'll stick to left/right.
Sorry, but I'm not sure how to do that. I tried this:
float horizontal = 0f;
// You can extend this class and override any of these virtual methods
protected virtual void $$anonymous$$ove(Vector2 horizontal)
{
// It's actually 2D vector
transformCache.position = transformCache.position + horizontal;
Face$$anonymous$$ovementDirection(horizontal);
}
private void Face$$anonymous$$ovementDirection(Vector2 direction)
{
if (direction.sqr$$anonymous$$agnitude > 0.1)
{
transform.up = direction;
}
But of course that doesn't work and gives me errors.
$$anonymous$$ore like this:
protected virtual void $$anonymous$$ove(Vector3 relativeVector)
{
Vecto2 pos = new Vector2(transformCache.position.x + relativeVector.x, transformCache.position.y);
transformCache.position = pos;
Face$$anonymous$$ovementDirection(pos);
}