- Home /
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 :)
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.
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.
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?
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.
Your answer
Follow this Question
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