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 deepak-impressol · Nov 21, 2012 at 07:17 AM · javascripttouchmoveprojectile

moving an object with swipe for ios?

Actually, in this code i am taking one ball as a object, i want to swipe that ball and move towards the goal post. and i want to set the angle for that ball according to X axis displacment and for projectile according to y axis displacment . and also i want to give the speed according to swipe of fingure on ball. please help me.

thank you for your help.

var Ball : GameObject; // football is taking as an object

 function Update ()
  {
 
         if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
                    {
 
                         var touchdeltaposition: Vector2 = Input.GetTouch(0).deltaPosition;
                         anglex = touchdeltaposition.x ;
                         angley = touchdeltaposition.y ;
                     
                
                   elevationAngle = Vector3(anglex,0,0); // angel for ball movment
                   var throwForce:int = 1000;
                 Ball.rigidbody.velocity = transform.TransformDirection(Vector3(anglex,angley, 0)); // projectile of ball on the plan
                 var elevation : Vector3 = Quaternion.Euler(elevationAngle) * transform.forward; 
                 ball.rigidbody.AddForce(elevation * throwForce);
                 
                  Debug.Log("touch end"); 
                       }
                
                
                    
                
 }
     
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Graham-Dunnett · Nov 21, 2012 at 11:00 AM

I've no idea what your code is supposed to do. Perhaps edit your question and describe step by step what that code is meant to do. Here's how I would tackle the same problem (not that I have ever attempted to write this code..)

I'm guessing that your screen has the goal visible, and there is a ball somewhere on the screen. The top and bottom of the two goal posts define 4 points in 3d space, and 4 points in 2d screen space. The ball defines a 5th position in 3d space, and 5th position in 2d screen space. If I did a short swipe from the ball towards say the top right hand goal post, I'd want the ball to be kicked in that direction, just not very fast. If instead I did a longer or faster swipe to the same point, then that would be a stronger kick. So, I'd look to see where the swipe started and ended, and how long that took. This gives me some idea about the speed of the kick. Next, I'd work out from the 4 2d screen coordinates representing the top and bottom of the goal posts, and the end point of the swipe where in the 3d space of the goal the goal the ball needs to be kicked. This then gives you a direction to kick the ball.

Comment
Add comment · Show 1 · 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
avatar image deepak-impressol · Nov 22, 2012 at 06:20 AM 0
Share

thanks for your replay sir, i edited my question. the main problem is that i want to move the ball on 3d space according to our touch. like that " throw a ball towards Z direction with speed and angular projectile of that ball on plane". i hope that now you are understanding my problem.

and again thanks for your valuable reply.

avatar image
-1

Answer by alok-kr-029 · Jan 17, 2014 at 06:40 AM

Not Working

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
avatar image
0

Answer by deepak-impressol · Nov 24, 2012 at 09:12 AM

finally i solved my problem. and i got perfect direction of ball . i am putting my code , may be some people can use it . thanks. var anglex : float; var angley : float; var elevationAngle : Vector3; function Awake () { if (ScreenOrientation.Landscape) { Screen.orientation = ScreenOrientation.LandscapeLeft; } else { Screen.orientation = ScreenOrientation.Portrait; }

  }
 
 function FixedUpdate ()
  {
 
         if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
                    {
 
                         var touchdeltaposition: Vector2 = Input.GetTouch(0).deltaPosition;
                         anglex = touchdeltaposition.x * Time.deltaTime *2.0;
                         Debug.Log(anglex);
                         angley = touchdeltaposition.y * Time.deltaTime *5.0;
                         Debug.Log(angley);
                         
                 elevationAngle = Vector3(anglex,angley,0); 
              
                 rigidbody.velocity = transform.TransformDirection(Vector3(anglex,angley,angley ));
                 var elevation : Vector3 = Quaternion.Euler(elevationAngle) * transform.forward; 
                 rigidbody.AddRelativeForce(elevation);
                 
                  Debug.Log("touch end"); 
                       }
                
                }
Comment
Add comment · Show 1 · 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
avatar image namekaw · Jun 28, 2013 at 08:53 PM 0
Share

Thanks for the script. I have tried using this script on a car though when I swipe nothing happens. I am trying it on unity remote android. If I assign it to the certain game object does it also work on other object (not desired). Is there something special that needs to be done because my car does not move when swiped in remote unity. Thanks (realise it is an old topic!)

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

13 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

Related Questions

Please tell me what is wrong with this Projectile.js file 1 Answer

How to get my character to move left to right? 0 Answers

Touch teleport script isnt working 2 Answers

A node in a childnode? 1 Answer

Make an object move in the direction of touch 0 Answers


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