Question by
lotdev53 · Jun 04, 2020 at 03:35 PM ·
touchmovement scripttouchscreen
move character by touch only x axis
even my finger is stay still but character still move and it is not smooth. here is my code Thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playerscript : MonoBehaviour
{
float oldPos;
public float speed = 10f;
private Vector3 moveDirection = Vector3.zero;
CharacterController Player;
void Start()
{
Player = GetComponent<CharacterController>();
}
private void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
oldPos = touch.position.x;
moveDirection = new Vector3(0.0f, 0.0f, 0.0f);
break;
case TouchPhase.Moved:
if (oldPos > touch.position.x)
{
moveDirection = new Vector3(1 * speed, 0.0f, 0.0f);
Debug.Log("go left" );
}
else
{
Debug.Log("go right" );
moveDirection = new Vector3(-1 * speed, 0.0f, 0.0f);
}
oldPos = touch.position.x;
break;
case TouchPhase.Ended:
moveDirection = new Vector3(0.0f, 0.0f, 0.0f);
break;
}
}
}
void FixedUpdate()
{
Player.Move(moveDirection * Time.deltaTime);
}
}
Comment
Your answer

Follow this Question
Related Questions
Opposite moving object? 0 Answers
Pausing Play on Android 0 Answers
Rotate object on mobile touch,How I can rotate an object more times? 0 Answers
Simulated Touchscreen of the new input system problems 0 Answers
How to optimize touch input script 0 Answers