- Home /
Move an object to Input.Touch location
Hey all,
I'm trying to set up my code so that an (petal)object will float towards (and eventually circle) the point of Touch.Input.
My method is -
1) Determine if the input is a touch or press by using a timer. 2) If the input is a press, then I use a RayCast / triggerbox to find the touch coordinates as a Vector3.
3)Instantiate a game object called "attractor" at the hit point. If the attractor is within a certain distance of my petal, then the petal floats over to the attractor's position.
Here is the code:
using UnityEngine;
using System.Collections;
public class Flick : MonoBehaviour {
public GameObject attractorPrefab;
public GameObject exploderPrefab;
public GameObject petal;
public float holdCount;
public float holdSpeed;
public float coolDown;
public float speed;
public float startTime;
float swipeSpeed;
float inputX;
float inputY;
public int count;
public int tapCount;
public int doubleTap;
public bool pull;
public bool holdState;
public bool attractorPresent;
public Vector3 hp;
Vector3 startPosition;
Vector3 touchPosition1;
Vector3 touchPosition2;
Touch touch;
void Start ()
{
count = Input.touchCount;
petal = GameObject.Find("petal");
//attractorPrefab = GameObject.FindGameObjectWithTag("Attractor");
attractorPresent = false;
holdCount = 0.0f; //determine whether input is a tap or hold
pull = false;
startTime = Time.time;
holdState = false;
holdSpeed = 0.001f;
speed = 0.5f;
coolDown = 0; //input delay counter
}
void Update ()
{
coolDown -= Time.deltaTime;
if (coolDown < 0)
coolDown = 0;
int i = 0;
if((Input.touchCount > 0) && (coolDown == 0))
{
holdCount +=Time.deltaTime;
if(holdCount > 0.7f)
{
holdState = true;
//determine position of input
Ray ray = camera.ScreenPointToRay(new Vector3(Input.GetTouch(0).position.x,
Input.GetTouch(0).position.y,
0));
RaycastHit hit;
if((Physics.Raycast(ray, out hit)) && (attractorPresent == false))
{
Vector3 hp = hit.point;
hp.z = 0;
//instantiate attractor
Instantiate(attractorPrefab, hp, Quaternion.identity);
attractorPresent = true;
Debug.Log ("Attractor call.");
}
if(Input.GetTouch(0).phase == TouchPhase.Ended)
{
holdState = false;
holdCount = 0;
attractorPresent = false;
Debug.Log("Hold finished");
}
}
else
if ((Input.GetTouch(0).phase == TouchPhase.Ended) && (holdState == false))
{
Debug.Log("Instantiate explosion");
holdCount = 0;
//touchPosition = new Vector3(touchPosition.x, touchPosition.y, 0);
Ray ray = camera.ScreenPointToRay(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0));
//Input.GetTouch(0).position.z = 10;
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Vector3 hp = hit.point;
hp.z = 0;
//Create explosion
Instantiate (exploderPrefab, hp, Quaternion.identity);
holdCount = 0.0f;
Debug.Log(hit.point);
}
}
}
}
void HoldCountStart()
{
holdState = true;
Debug.Log("holdCount starts");
}
void AddExplosion()
{
holdCount = 0;
Debug.Log("Count finished.");
}
}
/*if(Input.GetTouch(0).tapCount >= 2)
{
if(Input.GetTouch(0).phase == TouchPhase.Began)
{
doubleTap++;
}
}*/
/* touchPosition = new Vector3(touchPosition.x, touchPosition.y, 0);
Ray ray = camera.ScreenPointToRay(new Vector3(touchPosition.x, touchPosition.y, 0));
touchPosition.z = 10;
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Vector3 hp = hit.point;
hp.z = 0;
//Create the explosion prefab
Instantiate (exploderPrefab, hp, Quaternion.identity);
holdCount = 0.0f;
Debug.Log(hit.point);
}
*/
//swipe input
/*void FixedUpdate()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
inputX += touchDeltaPosition.x * swipeSpeed;
inputY += touchDeltaPosition.y * swipeSpeed;
Debug.Log("X, Y: " + touchDeltaPosition.x + ", " + touchDeltaPosition.y);
}
}*/
//Check for input type
//if(Input.Touch)
//{
//count += Time.deltaTime;
/*if (count > 0.7f)
{
pull = true;
mousePosition = Input.mousePosition;
Ray ray = camera.ScreenPointToRay(new Vector3(mousePosition.x, mousePosition.y, 0));
mousePosition.z = 10;
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) && attractorPresent
{
Vector3 hp = hit.point;
hp.z = 0;
Instantiate(attractorPrefab, hp, Quaternion.identity);
attractorPresent = true;
}
}
}
//petal.transform.position = Vector3.Lerp(petal.transform.position,
// attractorPrefab.transform.position,
// Time.deltaTime * speed);
*///}
And the Attractor.cs -
using UnityEngine;
using System.Collections;
public class Attractor : MonoBehaviour {
Flick flick;
public GameObject petal;
public bool holdState;
Transform target;
public float speed;
public float distance;
Vector3 endPoint;
private Vector3 startPoint;
private float startTime;
// Use this for initialization
void Start ()
{
Debug.Log("Attractor has entered the building.");
petal = GameObject.Find("petal");
GameObject MainCamera = GameObject.Find("Main Camera");
endPoint = transform.position;
startPoint = petal.transform.position;
startTime = Time.time;
flick = MainCamera.GetComponent<Flick>();
holdState = flick.holdState;
speed = 5.0f;
}
// Update is called once per frame
void Update ()
{
distance = Vector3.Distance(transform.position, petal.transform.position);
if (distance <= 5)
{
petal.transform.position = Vector3.Lerp(transform.position, flick.hp, Time.deltaTime * speed / distance);
}
if((Input.GetTouch(0).phase == TouchPhase.Moved) || (Input.GetTouch(0).phase == TouchPhase.Ended))
{
AttractorDestroyer();
}
}
void AttractorDestroyer()
{
DestroyObject(this.gameObject);
}
}
Now the problem I get is that the petal won't stop at the point that I've touched. It continues to advance. I imagine that the problem is that my raycast is coming from my MainCamera and the main camera's transform.position is tied by a separate script to the petal's transform.position (since I need the camera to follow the petal.)
TO be honest, I'm not sure how to fix this. I need the petal to float over to the point that has the hold input (and then circle around that point.) Any suggestions on how I can do this?
Thanks for the time!
Your answer
Follow this Question
Related Questions
Movement along with touch of finger 2 Answers
Drag an object to touch position(Problem) 1 Answer
Help With Touch to Drag Script 1 Answer
Odd touch input problem 0 Answers