Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 nandonandito69 · Dec 12, 2014 at 12:51 PM · c#instantiate

touch to instantiate and move object ?

when i touch my screen is instantiate object maybe like quad or cube and i can move thats object if i never release my touch, but if i released my touch thats object is cant move again. and if i give touch again, im instantiate and move object always. how can i try this? because when i make script at camera like OnMouseDown its cant work? thankyou

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CorruptedTNC · Dec 13, 2014 at 02:04 PM

Touching to spawn objects is not really something you want to happen if you want to be able to move placed objects later on. Here's what I would do personally:

Have buttons that spawn different Game Objects and then keep a reference for them for example:

 public GameObject curObject;
 public LayerMask objLayer;
 if(GUILayout.Button("Spawn Cube"))
 {
     curObject = GameObject.CreativePrimitive(PrimitiveType.Cube);
 }

Touch controls come with phases, which give you access to information similar to events, which you can use as follows:

 void Update()
 {
     //This will get the first touch available when the finger is moving
     if (Input.GetTouch(0).phase == TouchPhase.Moved && curObject != null)
     {
         //This may be incorrect, I don't have Unity in front of me.
         //If this is wrong, look at transforming this to a world co-ordinate.
         curObject.transform.position = Input.GetTouch(0).Position;
     }
     //The finger has left the screen and we have a curObject that we were moving
     if(Input.GetTouch(0).phase == TouchPhase.Ended && curObject != null)
     {
         //Set up a layer specifically for movable GameObjects and set that
         //layer number here, you'll see why in a second
         curObject.Layer = 6;
         //We're done with the current object, so our reference to it must now be
         //removed
         curObject = null;
     }
     //We don't have a current object, time to find one
     if (Input.GetTouch(i).phase == TouchPhase.Began && curObject == null) 
     {
         // Construct a ray from the current touch coordinates
         Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, Mathf.Infinity, objLayer))
         {
             //We've hit something, information is in the RaycastHit object.
             curObject = hit.transform.GameObject;
         }
     }
 }

Things for you to read: Raycast information RaycastHit information Layers and LayerMasks

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Player instantiates Backwords 0 Answers

Instantiate object ahead of player 0 Answers

Facebook SDK and ADColony Compatible in iOS? 2 Answers

What should I change in this instantiating a GameObject in a script using Unity? 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