- Home /
hold touch to move, tap to rotate
public float holdTime = 4f;
void Update()
{
Move();
}
public void Move()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
float startTime = Time.time;
if (startTime >= holdTime)
{
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime);
}
}
}
I need to if touch hold 2 seconds character moved to the touch spot and if just one tap turned towards to spot
Answer by aditya · Oct 20, 2016 at 08:54 AM
bool wasLongTouch;
public void Move(){
if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began){
Invoke("changeBool", 2f);
}
if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended){
if(!wasLongTouch){
// Just Rotate the player
CancelInvoke("changeBool");
}else{
// Long Press Detected, move player to touch position
}
}
void changeBool(){
wasLongTouch = true;
}
}
Tip : disable touch when player is moving or rotating ... and when there was a long touch do not forget to change wasLongTouch bool back to false when player finished moving
Don't copy paste this in your code as this is not tested
public void $$anonymous$$ove()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Invoke("changeBool", 2f);
}
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (!wasLongTouch)
{
// Just Rotate the player
CancelInvoke("changeBool");
}
else
{
// Long Press Detected, move player to touch position
}
}
}
public void changeBool()
{
wasLongTouch = true;
}
}
nope, i try to rewrite code using invoking didn't work...
Dont use TouchPhase.Stationary, use TouchPhase.Began
when phase == began it moves just a moment and stop to move i use this
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime);
update: If you are stationary holding finger on screen player start moving to that point.
public float duration = 10f;
public float rotationSpeed = 5f;
public float tapHolding;
private bool flag = false; //Set to true on click. Reset to false on reaching destination
private float zAxis; //vertical position of the gameobject
private Vector3 endPoint; //destination point
private float nextFire;
// Use this for initialization
void Start()
{
zAxis = gameObject.transform.position.z; //save the y axis value of gameobject
}
// Update is called once per frame
void Update()
{
$$anonymous$$ove();
}
public void $$anonymous$$ove()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
tapHolding += Time.deltaTime;
if (Physics.Raycast(ray, out hit) && tapHolding >= 2)
{
flag = true;//set a flag to indicate to move the gameobject
endPoint = hit.point; //tap position
endPoint.z = zAxis; //as we do not want to change the z axis value based on touch position, reset it to original z axis value
Debug.Log(endPoint);
//check if the flag for movement is true and the current gameobject position is not same as the tapped position
}
}
else
{
tapHolding = 0;
}
if (flag && !$$anonymous$$athf.Approximately(transform.position.magnitude, endPoint.magnitude))
{
//move the gameobject to the desired position
float targetAngle = $$anonymous$$athf.Atan2(endPoint.y, endPoint.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), rotationSpeed * Time.deltaTime);
transform.position = Vector2.Lerp(transform.position, endPoint, 1 / (duration * (Vector2.Distance(transform.position, endPoint))));
tapHolding = 0;
}
//set the movement indicator flag to false if the endPoint and current gameobject position are equal
else if (flag && $$anonymous$$athf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
flag = false;
Debug.Log("I am here");
}
}
}
Answer by Easy_ · Oct 20, 2016 at 10:15 AM
void Update()
{
Move();
}
public void Move()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
float startTime = Time.time;
if (startTime >= holdTime)
{
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime);
}
}
}
}
i have this code, when enter in playmode character move to touch point after two seconts hold, but then i need to reset timer called "startTime" after touch is ended. Then do another touch hold to move after 2 second and so on.