script to move the character to the left, right, jump and slide .... with your finger sliding on the phone screen (swipe)
I'm trying to create a script for a character running to move left, right, jump and slide your finger sliding on the phone screen (swipe), but I'm in trouble !!
private float ponteiroX, ponteiroY, novaPosicX;
private int indicePosic;
private bool podeMover, estaNoChao, pulouR;
private Vector3 posicInicial;
[Range(0.01f, 1)]
public float TempoParaMover = 0.15f;
[Range(1, 5)]
public int QuantoMover = 1;
[Range(1, 20)]
public float forcaDoPulo = 5.0f;
public LayerMask LayersNaoIgnoradas = -1;
private Rigidbody corpoRigido;
void Start()
{
corpoRigido = GetComponent<Rigidbody>();
corpoRigido.constraints = RigidbodyConstraints.FreezeRotation;
posicInicial = transform.position;
novaPosicX = posicInicial.x + indicePosic * QuantoMover;
indicePosic = 0;
pulouR = false;
podeMover = true;
}
void Update()
{
estaNoChao = Physics.Linecast(transform.position, transform.position - Vector3.up, LayersNaoIgnoradas);
if (podeMover)
{
DetectarMovimento();
}
}
IEnumerator EsperarParaMover(float tempo)
{
yield return new WaitForSeconds(tempo);
podeMover = true;
}
IEnumerator EsperarParaPular(float tempo)
{
yield return new WaitForSeconds(tempo);
pulouR = false;
}
void DetectarMovimento()
{
podeMover = false;
StartCoroutine(EsperarParaMover(TempoParaMover));
ponteiroX = ponteiroY = 0;
if (Input.GetMouseButton(0))
{
ponteiroX = Input.GetAxis("Mouse X");
ponteiroY = Input.GetAxis("Mouse Y");
}
if (Input.touchCount > 0)
{
ponteiroX = Input.touches[0].deltaPosition.x;
ponteiroY = Input.touches[0].deltaPosition.y;
}
//DETECTAR EIXO X
if (ponteiroX > 0 && indicePosic < 1)
{
indicePosic++;
novaPosicX = posicInicial.x + indicePosic * QuantoMover;
}
else if (ponteiroX < 0 && indicePosic > -1)
{
indicePosic--;
novaPosicX = posicInicial.x + indicePosic * QuantoMover;
}
//DETECTAR EIXO Y
if (ponteiroY > 0.1f)
{
Pular();
}
}
void FixedUpdate()
{
Vector3 proximaPosic = new Vector3(novaPosicX, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, proximaPosic, Time.deltaTime * 5);
}
void Pular()
{
if (estaNoChao == true && pulouR == false)
{
corpoRigido.AddForce(Vector3.up * forcaDoPulo, ForceMode.Impulse);
pulouR = true;
StartCoroutine(EsperarParaPular(0.5f));
}
}
}
There is no question here. No indication of what your "trouble" is. No direction given for anyone to even being trying to help you. Add more details. Refer to the FAQ and User Guide linked on the right for help on asking better questions.
The character moves off in the direction "Z", I wanted to add a script in character so that when sliding your finger on the mobile screen to the right it moves to the right, and do the same thing to the left.
That's the expected behavior. What behavior are you actually seeing in your script? Have you used Debug.Log to look at various values? Have you used the debugger to step through the code to find out what is happening?