- Home /
TouchScreen
Dear Sir/Madam i got a problem with "how to control my game to move up,down,left and right" .The first I control my moving game with button key(left,right,up and down).But I want to control my moving game with touch on screen.so how can i solve it? please see my code to control on Key Button. +++++++++++++++++++++++++++++++Code+++++++++++++++++++++
public void playerMove() {
GameObject Moverment = GameObject.Find("Player");
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
Moverment.rigidbody.velocity = movement * SingletonManage.Instance.gameData_Ref.speed;
Moverment.rigidbody.position = new Vector3
(
Mathf.Clamp(Moverment.rigidbody.position.x, SingletonManage.Instance.gameData_Ref.xMin, SingletonManage.Instance.gameData_Ref.xMax),
7f,
Mathf.Clamp(Moverment.rigidbody.position.z, SingletonManage.Instance.gameData_Ref.zMin, SingletonManage.Instance.gameData_Ref.zMax)
);
Moverment.rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, Moverment.rigidbody.velocity.x * -(SingletonManage.Instance.gameData_Ref.tilt));
}
Hey, you can find examples in our documentation for mobile device input here: http://docs.unity3d.com/$$anonymous$$anual/$$anonymous$$obileInput.html
Simple example here: http://docs.unity3d.com/ScriptReference/Input-touches.html
There is a recent live session video, showing how the process to simple create a touch control. (using the space shooter scenes). The video is hosted by $$anonymous$$Buckner.
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/space-shooter-to-mobile
Answer by zharik86 · Jan 19, 2015 at 07:49 PM
Simple way: create GUITexture as joystick for touch. Than (write on CSharp):
public GUITexture myMoveBtn = null; //change, reference on your gui texture
private int finId1 = -1; //id finger for cancel touch event
private GameObject Moverment = null;
//UPDATE 1: use global variable for your player object
void Start() {
//Only one time find your game object
Moverment = GameObject.Find("Player");
}
void Update() {
//Check count of touches
if(Input.touchCount > 0) {
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began && myMoveBtn.HitTest(touch.position) && finId1 == -1) {
finId1 = touch.fingerId; //store Id finger
//Do stuff
}
//Move
if(touch.fingerId == finId1) {
//Calc right angle
//UPDATE 1: using center of GUITexture
float ang = Mathf.Atan2(touch.position.x - myMoveBtn.GetScreenRect().center.x, touch.position.y - myMoveBtn.GetScreenRect().center.y)
//your code
float moveHorizontal = 1.0f * Mathf.Sin(ang);
float moveVertical = 1.0f * Mathf.Cos(ang);
//An etc your code, where you calculate move
//...
}
if (touch.phase == TouchPhase.Ended) { //correct end of touches
if(touch.fingerId == finId1) { //check id finger for end touch
finId1 = -1;
//UPDATE 1: stop rigidbody, when you release GUITexture
Moverment.rigidbody.velocity = new Vector3(0, 0, 0); //you can use "Vector3.zero" too.
Moverment.rigidbody.angularVelocity = Vector3.zero;
}
}
}
}
}
I hope that it will help you.
P.S.: Realy touch checks only real device or Eclipse emulator of android (AVD).
Thank you! For answer this problem and i also try to follow you but something have err.. So can you detail it for me ? my$$anonymous$$oveBtn.HitTest(touch.position)
my$$anonymous$$oveBtn.GetScreeRect().x
Oh, sorry, "my$$anonymous$$oveBtn" must have another type: GUITexture. I change it in my answer.
Oh.. Thank you and try to follow that and have 1 more err..
my$$anonymous$$oveBtn.GetScreeRect().x, my$$anonymous$$oveBtn.GetScreeRect().y
Ok, ins$$anonymous$$d "GetScreeRect", write "GetScreenRect". I update this. Hope, no error:)
Answer by MarlonH · Jan 20, 2015 at 11:37 AM
Hello! You can use "Imput.TouchCont" and "Imput.GetTouch (int Touch)" to work with touchs. Within the GetTouch, you can take the position of the touch (Imput.GetTouch (0) .position.x, for example). :)
Answer by Ali_unity · Jul 23, 2015 at 12:31 AM
The easiest way to implement touch controls for newbies like us is the asset "easy touch controls" in the asset store for 10$. I am new to unity and programming and this asset worked for me without any problem. that's very easy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180843im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to identify physical objects on a touch screen. 4 Answers
TouchScreenKeyboard.isSupported==false 1 Answer
Touch input not working 3 Answers
Get position of touch 2 Answers
Scripting for UI Buttons/Images 0 Answers