• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BlacKatFever · Nov 15, 2011 at 08:02 AM · raycastinputvector3touchtransform.position

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 $$anonymous$$t point. If the attractor is wit$$anonymous$$n 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 $$anonymous$$t;
                 
                     if((Physics.Raycast(ray, out $$anonymous$$t)) && (attractorPresent == false))
                 {
                     Vector3 hp = $$anonymous$$t.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 $$anonymous$$t;
                         
                             if(Physics.Raycast(ray, out $$anonymous$$t))
                             {
                                 Vector3 hp = $$anonymous$$t.point;
                                 hp.z = 0;
                     
                                 //Create explosion
                                 Instantiate (exploderPrefab, hp, Quaternion.identity);
                                 holdCount = 0.0f;
                                 Debug.Log($$anonymous$$t.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 $$anonymous$$t;
                 
                 if(Physics.Raycast(ray, out $$anonymous$$t))
                 {
                     Vector3 hp = $$anonymous$$t.point;
                     hp.z = 0;
                         
                 //Create the explosion prefab
                     Instantiate (exploderPrefab, hp, Quaternion.identity);
                     holdCount = 0.0f;
                     Debug.Log($$anonymous$$t.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 $$anonymous$$t;
                 
                 if (Physics.Raycast(ray, out $$anonymous$$t)) && attractorPresent
                 {
                     Vector3 hp = $$anonymous$$t.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 t$$anonymous$$s 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(t$$anonymous$$s.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 t$$anonymous$$s. 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 t$$anonymous$$s?

Thanks for the time!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Given 2 input floats (horizontal, vertical), cast a ray orthogonal to the normal of the surface in direction of input floats? 0 Answers

Getting raycast to fire at touch position but only when second finger is on screen 0 Answers

Falling object touch 0 Answers

Detect if dragged over object 1 Answer

Touching Falling Objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges