Hello everybody,
I am a newbie in unity iphone development.
I just want to implement a joystick that would get active wherever user touches the screen.
I tried to implement it by using unity iphone inbuilt joystick and some of my coding in other script but the joystick gets available for a second and then restores back to its original position due to the reset coding written in Joystick.js.
Here is the other script that i used to make joystick active based on the touch position:
using UnityEngine;
using System.Collections;
public class touchpad : MonoBehaviour {
// Use this for initialization
GameObject lft;
void Start () {
lft=GameObject.Find("LeftJoystick");
}
Rect lastloc;
// Update is called once per frame
void Update ()
{
foreach(iPhoneTouch touch in iPhoneInput.touches)
{
if(touch.phase==iPhoneTouchPhase.Began)
{
lft.active=true;
lastloc=new Rect(touch.position.x,touch.position.y,100,100);
((GUITexture)lft.GetComponent(typeof(GUITexture))).pixelInset=lastloc;
}
else if(touch.phase==iPhoneTouchPhase.Moved)
{
((GUITexture)lft.GetComponent(typeof(GUITexture))).pixelInset=lastloc;
lft.active=true;
}
else if(touch.phase==iPhoneTouchPhase.Ended)
{
lft.active=false;
}
}
}
}
If anybody has played carnival then h/she must know what type of joystick i want to implement.
Please help me out. please.
Any help would be much appreciated.
Thanks
First of all i pay my sincere thanks to you for replying to my post. Frankly speaking i lost the hope of getting any reply on this topic.
Anyways, i tried the solution you have mentioned but it too didn’t work. Actually it disturbs the actual functionality of the joystick. The joystick doesn’t get reset to its center position.
Please provide me a better solution.
One more thing if u cud help me in this, is there any proble with Unity iPhone interface. Actually the same things that were working previuosly stops working by themselves. Lets just take the eg of joystick when i make any changes in the “JoyStick” script and reset them to defaults(after seeing the changes). But it starts woking in a weird manner.
I too am working on getting a functioning version of a C# script doing something similar. Having the joystick appear on the screen and react appropriately to the new relative coordinates(given the iPhone/iPad “touch” input).
Have you found any working solutions(in JS I assume)? Or better yet C# solutions?
I like the aaronblohowiak penelope converted joystick script, but wish I could get it to disappear and reappear(then function properly) given the user’s touch location on the screen, if that makes sense.
Hey zhx! I have posted the coding of the script that i used to implement that joystick but it didn’t work.
I wish and hope somebody posts a solution to this problem.
Hey do u know how to implement SWIPE.
I tried to use rigidbody.AddForce mehtod based on the direction of the swipe(although i’m still working on it) but i think there must be some other solution as well. If you do know about this please post here.
I hope someone with some skills and the will to help out can enlighten us as to how to make a functioning version of a popup “joystick” to control the character movement.
I get that the joystick should be instantiated at the iPhone touch screenspace location, but I’m having a bit of trouble making all input of the joystick relative to EACH new instantiation point for the joystick spawning on the screen on iphone touch input.
As for a swipe, I’m guessing a time.deltaTime based distance/velocity check could give you swipe style input, just my first guess. The time it takes for the iPhone touch input to go from one place on the screen space to another divided by the time.DeltaTime, or something like that.
Not sure what you’re looking for. You first say you don’t want it to reset, then when andeeee tells you how to fix it, you say you want it to reset.
Hey elveatles!
I tried to give the position of the Joystick at runtime via GUIPixel Inset but somehow(don’t know why) the Joystick is flickering to its original (0,0) position.
I hope you would understand.
If you could provide any script to implement this then it would be great.
When it flickers, does it do it really quickly then fix itself, or does it continuously flicker? If it’s the first one, I’m guessing you can set it’s GUIPixel Inset, then yield a frame (http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html) before you set active to true. This way, it won’t be visible until after it’s position has changed for that one frame.
@ elveatles
thanks for the code snippet, I hope this’ll work for me too.
@ ravinder
if you are still looking for swipe control, try this out to save you some time, if you’re interested(till you learn how to do it as you prefer) http://gameassets.net/swipeControl.html
elveatles. Thank you very much for positing the code here. The joystick flickers very quickly. As i activate it on touch.
The link that you have posted is broken.
Anyways i’ll try what you have advised.
and zhx,thanks for providing the link. But its not free, its saying to purchase the code.
Anyways,I really appreciate everybody’s support here.
Thanks
If my problems get solved i’ll definitely post a reply here till then if anybody gets any kinda solution then do provide me here.
Thanks again
Thanks for offering to post the working version when you find the solution, I’m still searching/working on it too. I look forward to your solution post
I tried this coding for SWIPE gesture. Please go through it. I may be long but please go through it for once.
using UnityEngine;
using System.Collections;
public class swipe : MonoBehaviour {
void Start ()
{
sphere=GameObject.Find("Earth");
}
GameObject sphere;
Vector3 temp,pos,start,end;
Ray ray;
static float starttime,endtime;
static bool touched=false;
void Update ()
{
foreach(iPhoneTouch touch in iPhoneInput.touches)
{
temp=new Vector3(touch.position.x,touch.position.y,4);
ray=camera.ScreenPointToRay(temp);
pos=camera.ScreenToWorldPoint(temp);
if(touch.phase==iPhoneTouchPhase.Began)
{
if(Physics.Raycast(pos,Vector3.up ,0.38f))//To get whether the touch is on Object
{
starttime=Time.time;
start=sphere.transform.position;
touched=true;//To call in the Ended phase that the object was touched
sphere.rigidbody.Sleep();//To reduce the force on this rigidbody
}
}//end of Began
if(touch.phase==iPhoneTouchPhase.Moved)
{
if(Physics.Raycast(pos,Vector3.up ,0.38f))
{
sphere.transform.position=pos;
start=sphere.transform.position;
}
}//end of Moved
if(touch.phase==iPhoneTouchPhase.Ended)
{
/*Last position of touch*/
end= camera.ScreenToWorldPoint(new Vector3(touch.position.x,touch.position.y,4));
if(endtime-starttime<=2 touched==true)//a particular time to get swipe gesture
{
Vector3 pos1=new Vector3(end.x-start.x,end.y-start.y,4);
sphere.rigidbody.AddForce(pos1*50f);
touched=false;
}
}//end of Ended
}//end of foreach
}//end of update()
}//end of class
Actually i provided the motion in the object via AddForce of the rigidbody but i think it must be implemented in other ways also like Vector3.Lerp to give a smooth and specified distance.
And there is some problem with AddForce functionality that it keeps on adding the force. I do have provided the rigidbody.Sleep() but i don’t like the type of functionality it is actually giving. I want the swipe gesture like that of Spider: The Secret of Bryce Manor.
I like the swipe provided by them in the game.
Could anyone please help me in this?
For better reference please find the script in the attachment.
Thanks
Sorry zhx for late reply.
No not yet. Actually i left that topic cuz i got busy in a project that has been assigned to me. And i’m still working on it.
So i’ll try to gain this functionality after the completion of my project and post the solution if got any.
@Ravinder: Thanks for the update, hope your new project is a successful one! If I get it to work as well, I’ll post my result… still working on it though.
It essentially changes the position of that gameObject according to the world coordinates but GUITexture is not aligned to these coordinates so the position varies with that of your finger.
Moreover when you do gui.PixelInset=touch.position then also it does not provide you the actual position you want.
The reason may lie in the pivot or center point of GameObject and GUITexture. The axes you see are for GameObject and not for guiTexture.
Don’t know how and when it would be achieved.
Hey, I have finally got the solution to this problem.
Here is the code. I tried it with mousePosition but you can replace it with touch.position. I hope that works too.
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
var screenPos=Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.position.x=Mathf.Abs(screenPos.x);
transform.position.y=Mathf.Abs(screenPos.y);
}
}//end of Update