- Home /
How to make function respond to only the initial touch position.
In this game what I'm trying to accomplish is a sprite that rotates towards the touch position (which I have done) and have knock-back. The problem i have run into now is that it rotates if you hold your finger down on your screen. I'm trying to make it so that it only rotates and applies force to the initial touch position, and if they want to change that position for the user to take off his finger and touch somewhere else.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMove : MonoBehaviour
{
public float speed = 100;
public Transform ttouch;
public Rigidbody2D myRigidbody;
public float knock = 40;
// Start is called before the first frame update
void Start()
{
knock = knock * -1;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
ttouch.position = touchPosition;
Rotate();
Knockback();
}
}
void Rotate() {
//Rotate towards touch
Vector3 direction = ttouch.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
}
void Knockback() {
//Knock back applied when screen touched
myRigidbody.AddForce(transform.right * knock);
}
}
Answer by mchts · May 24, 2019 at 06:44 AM
I edited my answer as your needs. You should store touch position on initial touch. So your code should be like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMove : MonoBehaviour
{
public float speed = 100;
//public Transform ttouch;
public Rigidbody2D myRigidbody;
public float knock = 40;
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
// Start is called before the first frame update
void Start()
{
knock = knock * -1;
}
// Update is called once per frame
void Update()
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began){
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
//ttouch.position = touchPosition;
}
Rotate();
Knockback();
}
void Rotate() {
//Rotate towards touch
Vector3 direction = touchPosition - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
}
void Knockback() {
//Knock back applied when screen touched
myRigidbody.AddForce(transform.right * knock);
}
}
Thank You! This particular script did not completely work, but you showed me the Touch.phase which eventually solved it. I did happen to run into a couple errors though. One problem that came up was me not being able to access my touchPosition from my rotate function, which I ended up having to get rid of all together so it looks a little messy. Also the second is an Error which says: Argument Exception: Index out of Bounds. Seems like it has something to do with my touch position on screen or maybe the unity remote app.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Touch$$anonymous$$ove : $$anonymous$$onoBehaviour
{
public float speed = 100;
public Transform ttouch;
public Rigidbody2D myRigidbody;
public float knock = 40;
// Start is called before the first frame update
void Start()
{
knock = knock * -1;
}
// Update is called once per frame
void Update()
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
//ttouch.position = touchPosition;
Vector3 direction = touchPosition - transform.position;
float angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
$$anonymous$$nockback();
break;
}
}
void $$anonymous$$nockback()
{
//$$anonymous$$nock-back applied when screen touched
myRigidbody.AddForce(transform.right * knock);
}
}
A simple check will do this:
void Update() {
if(Input.touchCount > 0) //this line here will make you avoid index out of bounds errors
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
//ttouch.position = touchPosition;
Vector3 direction = touchPosition - transform.position;
float angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
$$anonymous$$nockback();
break;
}
}
}