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 TheJesusFish · Nov 20, 2012 at 12:35 AM · touch

Trying to track the direction of a swipe and then apply additional force to an object moving in that direction

I realize this question is probably ridiculously rudimentary, but this is literally the first batch of code I have written ever, in any language. After going through 2 or 3 coding and Unity3D books, I figured it was time to just sit down and start coding. Apologies in advance for my messy code and (probably) incorrect assumptions. I'll get it eventually :)

The gist of my prototype is I want to let the player swipe and object around the space, but I want the speed and thrust velocity to be controlled by a script. So, for instance, if the player swipes up, I want to get the normalized direction of the swipe, and then move the object in that direction. The catch is that I want the object to be powered by a motor that has nothing to do with the swipe speed or end position. So the script should grab the direction and say "Ok, I can move this object in that direction at the speed and power dictated in the script."

Hoping that makes sense, here is what I have so far. It is not complete, (the smash function is missing) but I wanted to know if I was on the right track

 #pragma strict
 
 var horizontalSpeed : float;
 var verticalSpeed : float;
 var thrust : float;
 var thrustsRemaining : int;
 var touch : Touch;
 var direction : Vector2;
 
 
 function Start () {
     Init ();
 }
 
 function Init () {
     thrustsRemaining = 2;
 }
 
 function Update () {
     if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved) {
         touch = Input.touches[0];
         CheckDirection ();
     }
 
 }
 
 function CheckDirection () {
     direction = touch.deltaPosition.normalized;
     
     if (touch.TouchPhase.Began.y < touch.TouchPhase.Ended.y || touch.TouchPhase.Began.y == touch.TouchPhase.Ended.y) {
         Thrust ();
     } else if (touch.TouchPhase.Began.y > touch.TouchPhase.Ended.y) {
         Smash ();
     }
 }
 
 function Thrust () {
     if (thrustsRemaining > 0) {
     transform.Translate (touch.x * thrust * horizontalSpeed * verticalSpeed * touch.y * time.deltaTime)
     thrustsRemaining--;
     }
 }
Comment
Add comment · Show 1
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 TheJesusFish · Nov 20, 2012 at 12:50 AM 0
Share

And yeah, I know that code is broken in more than one place. I'm fixing it as best I can. I was looking more for a point in the right direction, so to speak.

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

How to drag a GameObject with touch but at a limited speed 1 Answer

Multi Touch Drag Screen To Move Player? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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