Move GameObject to touchpoint with one touch
Hi there,
I am new to Unity and starting to learn more and more. Here is a thing i can't figure out. I want my GameObject to move smoothly to the point where I touch the screen (Android). I found a few examples, but these move the GameObject as long as my finger touches the screen. But I want the object to move when I shortly touch the screen. I tried to change TouchPhase.Stationary to TouchPhase.Began but then the GameObject will only move a bit in the right direction.
Thank you in advance for answering!!!!
void Update () {
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary)
{
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime);
}
}
}
Answer by jonahsrocket · Apr 02, 2016 at 08:42 AM
@Ali hatem Thank you for the fast respons. I get an error saying touch does not contain a definition for 'position' and no extension method 'position' accepting a first argument of type 'Touch' could be found
.
Any idea? I tried to figure this one out, but it's not working.
show me your script now i have tested my script with no problem.
This is the code I use now. Thank you for taking the time!
using UnityEngine;
using System.Collections;
public class TouchTest2 : $$anonymous$$onoBehaviour
{
// Use this for initialization
void Start()
{
}
Vector3 myposition;
void Update()
{
transform.position = Vector3.Lerp(transform.position, myposition, Time.deltaTime);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
myposition = touchPosition;
}
}
}
}
where is this line of code :
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
ok i get it you have deleted the wrong line i mean delete the TouchPhase test so it will fallow doesn't matter how you touch so delete this ins$$anonymous$$d
if (touch.phase == TouchPhase.Began){}
Answer by Ali-hatem · Mar 31, 2016 at 02:56 PM
Vector3 myposition;
void Update(){
transform.position = Vector3.Lerp(transform.position, myposition, Time.deltaTime);
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
myposition = touchPosition;
}
}