• 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 TheJesusFish · Jan 12, 2013 at 10:56 AM · iosinstantiation

Object Instantiation

I am still pretty new to Unity, and programming in general, and am having trouble with a drag control prototype script.

The easiest way to describe how I would like the script to function is almost like Angry Birds in reverse. You would touch the player object, and that would instantiate a targeting reticle. That reticle then followed your drag exactly, but has a max distance away from the player object. It would be like the slingshot in angry birds, only the slingshot would be the player character and the direction you dragged the bird is the direction you would move, with the force varying on the distance away from the player object you dragged. And, like angry birds, you can only drag a certain distance away from the player object before the target was fully extended.

I hope that makes sense. This is the script I have attached to the player object:

 #pragma strict
 
 private var startPoint : Vector3;
 private var endPoint : Vector3;
 var maxDistance : int;
 private var distance : Vector3;
 private var actualDistance : float;
 var direction : Vector3;
 var hit : RaycastHit;
 var ray : Ray;
 var thrustSpeed : int;
 var smashSpeed : int;
 
 var target : Transform;
 var player : Transform;
 
 
 function Start () {
 
     smashSpeed = 100;
     thrustSpeed = 50;
     maxDistance = 15;
 
 }
 
 function Update () {
 
     if (Input.touchCount == 1) {
         
         var touch = Input.touches[0];
         
 Debug.Log("Detected Touch");
         ray = Camera.main.ScreenPointToRay (touch.position);
     
         if (Physics.Raycast (ray, hit, 50) && hit.transform.name == "Player") {
              
             switch (touch.phase) {
             
                 case TouchPhase.Began:
 Debug.Log("Hit!");                    
                     startPoint = transform.position;
                     var liveTarget : Transform;
                     liveTarget = Instantiate(target, transform.position, transform.rotation);
                     endPoint = liveTarget.position;
                     distance = (endPoint - startPoint);
                     actualDistance = distance.magnitude;
                     break;
                     
                 case TouchPhase.Moved:
                     if (actualDistance > maxDistance) {
                         direction = distance.normalized;
                         endPoint = startPoint + direction * maxDistance;
                     }
                     break;
                     
                 case TouchPhase.Ended:
                     
 Debug.Log("Touch Ended");                    
                     Destroy(liveTarget);
                     break;
                     
             }
         
         }        
     }
 }


And this is the script I have attached to the target prefab that is instantiated (in theory) in the beginning of the touch phase.

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
         
         var touch = Input.touches[0];
         
         transform.Translate (touch.position);
     
     }
 
     if (Input.touchCount == 0) {
         Destroy(gameObject);
     }
 
 }

Right now, the console shows that it detects both a touch and a hit, but it never instantiates the reticle, let alone moving it around within the confines of the script. Any idea as to where I am going wrong?

Comment
Add comment · Show 2
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
avatar image cdrandin · Jan 12, 2013 at 11:35 AM 0
Share

Hey, sorry I don't have the answer to your question, but I am wondering how I could build games for iOS. Is it something different from Unity Basic? Just curious, mainly focusing on PC platform and never bothered to look it up. Just wondering as to how you are able to do it. Thanks in advance and sorry I couldn't answer your question.

avatar image liszto · Jan 12, 2013 at 12:45 PM 0
Share

@cdrandin why you don't create a question to have your answer ? :D

It's quite similar in the Engine with some change due to the input (indeed) and optimization due to mobile environment.

Your build with an iOS version generate a XCode project that is in Objective-C (Apple language for their apps) then you need to build your XCode project that generate an .app or what you want ;)

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

My unfortunate swimmer endlessly sinks. Help! 3 Answers

Help regarding World-Local Transfrom Rotation? 1 Answer

Axis oriented BRDF 0 Answers

[Solved] Change the limits on a hingejoint? 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