Hello, currently I am working on a 2D Sidescroller for Android with Input.GetTouch as a way to read the Input. But every time I stop touching the area which is used to move the Character, he continues to move in that direction even though I’m not touching anything.
void Update()
{
int Touches = Input.touchCount;
if(Touches > 0)
{
for (int i = 0; i < Touches; i++)
{
Touch touch = Input.GetTouch(i);
Movement(touch);
Jump(touch);
Crouch(touch);
}
}
// If there is no input then stop moving
if (LeftTouches <= 0)
{
MovementDirection = 0;
}
}
void Movement(Touch touch)
{
// Count the Touches
LeftTouches = 0;
if (IsMovingLeft(touch))
{
MovementDirection = -1;
LeftTouches++;
}
if (IsMovingRight(touch))
{
MovementDirection = 1;
LeftTouches++;
}
if (!IsCrouching(touch) && !WasCrouchJumping)
{
RB.velocity = new Vector2(MovementDirection * MovementSpeed, RB.velocity.y);
}
}
public bool IsMovingRight(Touch touch)
{
if (touch.position.x > Screen.width / 4 && touch.position.x < Screen.width / 2)
{
return true;
}
else
{
return false;
}
}
public bool IsMovingLeft(Touch touch)
{
if (touch.position.x < Screen.width / 4)
{
return true;
}
else
{
return false;
}
}
You are resetting LeftTouches in the Movement function wich is called only if there are active touch(es). Thus, when player releases stops touching, the value remains > 0.
That was a mistake but it’s still not fixed. I think it has something to do with the way Unity handles Touch Inputs. It emulates a mouse cursor and that cursor is still clicking that button even if you stop touching it.
No, this is not the case. You can disable mouse emulation and test again. If you issue isn’t fixed, then it means your scripts have more erorrs. Did you posted all the code related?
I tried turning off mouse simulation but that didn’t work. And that should be all the necessaire code but just in case here is the rest of it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MobilePlayerController : MonoBehaviour
{
[Header("Movement")]
public float MovementSpeed = 10;
[Header("Jump")]
public float JumpForce = 10;
public float JumpDelta = 1;
public int JumpTimes = 1;
[Header("IsGrounded")]
public float IsGroundOffSet;
public Vector2 IsGroundBoxSize;
[Header("Slide")]
public float SlideSize = 0.5f;
public float SlideDelta;
public float SlideSpeed;
public float SlideJumpMultiplyer;
[Header("Components")]
public Rigidbody2D RB;
public Collider2D BC;
// Private
//[Header("Later Private")]
bool WasCrouchJumping = false;
bool Jumped;
int TimesJumped;
float MovementDirection;
Vector2 StartPos;
Vector2 Direction;
bool DirectionChosen;
bool IsCrouched;
int LeftTouches;
private void Start()
{
Input.simulateMouseWithTouches = false;
}
void Update()
{
// Count the Touches
LeftTouches = 0;
int Touches = Input.touchCount;
if(Touches > 0)
{
for (int i = 0; i < Touches; i++)
{
Touch touch = Input.GetTouch(i);
Movement(touch);
Jump(touch);
Crouch(touch);
}
}
// If there is no input then stop moving
if (LeftTouches <= 0)
{
MovementDirection = 0;
}
}
void Movement(Touch touch)
{
if (IsMovingLeft(touch))
{
MovementDirection = -1;
LeftTouches++;
}
if (IsMovingRight(touch))
{
MovementDirection = 1;
LeftTouches++;
}
if (!IsCrouching(touch) && !WasCrouchJumping)
{
RB.velocity = new Vector2(MovementDirection * MovementSpeed, RB.velocity.y);
}
}
public bool IsMovingRight(Touch touch)
{
if (touch.position.x > Screen.width / 4 && touch.position.x < Screen.width / 2)
{
return true;
}
else
{
return false;
}
}
public bool IsMovingLeft(Touch touch)
{
if (touch.position.x < Screen.width / 4)
{
return true;
}
else
{
return false;
}
}
public void Jump(Touch touch)
{
if (IsGrounded())
{
if (Jumped)
{
WasCrouchJumping = false;
Jumped = false;
TimesJumped = 0;
}
if (IsJumping(touch))
{
TimesJumped++;
if (IsCrouched)
{
RB.AddForce(this.transform.up * JumpForce * SlideJumpMultiplyer, ForceMode2D.Impulse);
WasCrouchJumping = true;
this.transform.localScale = new Vector3(1, Mathf.Lerp(this.transform.localScale.x, 1, SlideDelta), 1);
IsCrouched = false;
}
else
{
RB.AddForce(this.transform.up * JumpForce, ForceMode2D.Impulse);
}
}
}
if (TimesJumped < JumpTimes && !IsGrounded())
{
if (IsJumping(touch))
{
TimesJumped++;
RB.AddForce(this.transform.up * JumpForce, ForceMode2D.Impulse);
}
}
if (!IsGrounded())
{
Jumped = true;
}
}
public void Crouch(Touch touch)
{
if (IsCrouching(touch))
{
if (IsGrounded())
{
RB.velocity = new Vector2(RB.velocity.x * SlideSpeed, RB.velocity.y);
}
this.transform.localScale = new Vector3(1, Mathf.Lerp(this.transform.localScale.x, SlideSize, SlideDelta), 1);
IsCrouched = true;
}
if (IsCrouching(touch) && touch.phase == TouchPhase.Ended)
{
this.transform.localScale = new Vector3(1, Mathf.Lerp(this.transform.localScale.x, 1, SlideDelta), 1);
IsCrouched = false;
}
}
public bool IsJumping(Touch touch)
{
if((touch.position.x > Screen.width / 2 && touch.position.y > Screen.height / 2))
{
return true;
}
else
{
return false;
}
}
public bool IsCrouching(Touch touch)
{
if ((touch.position.x > Screen.width / 2 && touch.position.y < Screen.height / 2))
{
return true;
}
else
{
return false;
}
}
public bool IsGrounded()
{
return Physics2D.OverlapBox(new Vector2(this.transform.position.x, this.transform.position.y - (BC.bounds.size.y/2 + IsGroundBoxSize.y + IsGroundOffSet)), IsGroundBoxSize, 0f);
}
}