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 apple741 1 · Dec 09, 2010 at 11:56 PM · javascriptphysicsiphoneoptimization

How to 'flick' objects in a 2d game

Hi,

I have my 2d iPhone game setup so that once a player touches the screen a ray is fired and if it hits an object the player can drag it around.

I was just wondering how I could go about allowing the user to 'flick' objects in Java script so that if they quickly touch an object and drag in a direction the object will move in that direction, but this depends on how much speed was put into the swipe - so the quicker the swipe the faster and further the objects will travel.

At the same time if they drag and hold the object will still follow the players finger as normal.

Thanks for your time :)

Comment
Add comment · Show 4
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 RJJ92 · Mar 02, 2015 at 05:21 AM 0
Share

Did you get a solution for this? I'm looking for the same.

avatar image Fornoreason1000 · Mar 02, 2015 at 05:42 AM 0
Share

Hi apple741 1, I think i have a half-baked solution.

You are aware of the new Unity Events system and the new UI system that has come into unity, I was working on something similar, A UI Joystick for a 2D touch game, sort of what you see in that COD Zombies game for Iphone.

This lead me to use the events that are called on other things such as Sliders. It took me FOREVER to get used to thanks to the incredibly poor or nonexistent docs, and a lack of tutorials. basically you will need these events

OnDrag, OnBeginDrag, OnEndDrag and OnDrop.

now to have them used we do this We inherit the various interfaces that the events are named after. to access them add the using statements i have provided.

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 using System.Collections;
 
 
 piblic class FlickableObject : $$anonymous$$onobehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler {
 
 
 public void OnBeginDrop(PointerEventData data)
     {
         
     }
 public void OnDrag(PointerEventData data)
     {
         
     }
 public void OnEndDrag(PointerEventData data)
     {
         
     }
 public void OnDrop(PointerEventData data)
     {
         
     }
 
 
 }

Now all it left is implementing it to control your object position and calculating it velocity to continue when you let go.

avatar image Fornoreason1000 · Mar 02, 2015 at 06:56 AM 0
Share

Ok it turns out $$anonymous$$onobehaviour doesn't invoke the Events i proposed earlier. I know Selectable does, but I'm assu$$anonymous$$g you aren't using UI object for this right?

if you aren't, you could implement, or recycle the event system i proposed somehow to work with all objects. According to THe manual it should work if you add a Physics 2D ray caster to the camera, but so far that's failed me dismally. which means you will have to build your own.

avatar image RJJ92 · Mar 02, 2015 at 07:01 AM 0
Share

Uh, no actually I've got a 3D game, but I want the ball to move on the ground only. And my ground is on the X and Z plane, but most codes online have a X and Y plane movement only. ANd Vector2 objects are usually X and Y. Vector3 confuses me. :S Any suggetions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fornoreason1000 · Mar 02, 2015 at 08:08 AM

i have my 2d iPhone game

No worries it just means you will use the Physics Raycaster Component and add it to the camera that renders your Ball, your ball MUST have a Rigid body AND a Collider or it won't work.

BTW I release i had some sort of memory problem with unity, which screwed up the physic, the raycaster actually do work. Back to it.

now basically in OnDrag you want to be constantly setting the position to the position of your player finger.

  public void OnDrag(PointerEventData data)
      {
           //even data is based on Screen View so no need to worry about the XY XZ thing
           rigidbody.velocity =  new Vector3(data.delta.x, 0, data.delta.y) * 10f
      }



use the others for honing the control of the ball. you'll notice that the ball will "lag" behind the users finger.

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

2 People are following this question.

avatar image avatar image

Related Questions

Make objects easier to select 1 Answer

How to support Retina display 1 Answer

Can I stop a lerp completely when it gets to 20% of its original value? 3 Answers

A node in a childnode? 1 Answer

ExecutionEngineException: Unity 3 on the iPhone 2 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